Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Difference static and non static, public and private filter_list
Author
Message
RE: Difference static and non static, public and private #31
Spoiler: Open
(10-25-2013, 10:49 AM)blackeagle Wrote: @Deque sorry to bother you Sad but i still have questions !!
while reading in @"Psycho_Coder" tut here http://docs.oracle.com/javase/tutorial/r...index.html
i was able to understand whats a superclass ,a sub class and how a subclass extends from a superclass.
but what i wasn't able to understand is what is interface i know that it's enpty methods but didnt understand why shall we have empty methods and why to implements from it !!
i'll be thanksfull if you can give me some exemples that helps me understand it

Interfaces are reference types. Interfaces provides flexibility and are a way to provide abstraction and multiple inheritance in java. In very simple language they provide a skeleton and once you have the skeleton you can create any being.

Now suppose you have the skeleton of a human then you can make a man, a women, a child, But you cannot make a tiger with that, since their skeleton are a different except some features. So you can implement the skeleton interface and implement the methods according to its need.

Now we know that Interfaces cannot be extended by a class but can be implemented as needed. But another interface can extend another another interfaces. So an interface can be entended by another interface for added functionality

Let look at the following (I will consider the animal kingdom to explain you interfaces.)

Lets suppose we have a interface :-

Code:
public interface Skeleton{

/**
*@return the number of bone present in the body
*/
public abstract int noOfBones(String animalSpecies);

/**
*@return the average height of the the animalSpecies
*/
public abstract int averageHeight(String animalSpecies);


/**
*@return the average weight of the the animalSpecies
*/
public abstract int averageWeight();

/**
*@return an array of the characteristics of the animalSpecies
*@param animalSpecies The species class according to which the characteristics may change
*/
public abstract String[] characteristics(String animalSpecies);

}

Here you see that the Skeleton interfaces has 4 abstract methods. Note that an interface implicitly considers the methods to be abstract.

You see that we now since we have the skeleton interface so we can use it in different places for different class of animals. Such as Mammals, aquatic, aerial etc. we just need to pass the class the of the animal and then we can use these methods repeatedly. You can say that I can recreate the methods whenever needed but would that be efficient, NO!. It would consume more space, would have performance issues, it would make the code less readable. Using interfaces makes your code readable.

Lets get back to our interface. You see that we are passing a common String value to every method. If we want to pass other types as well. To achieve this we create pass some values to the interface. See the following code :-

Code:
public interface Skeleton<E> {

    /**
     * @return the number of bone present in the body
     */
    public abstract int noOfBones(E animalSpecies);

    /**
     * @return the average height of the the animalSpecies
     */
    public abstract int averageHeight(E animalSpecies);

    /**
     * @return the average weight of the the animalSpecies
     */
    public abstract int averageWeight(E animalSpecies);

    /**
     * @return an array of the characteristics of the animalSpecies
     * @param animalSpecies
     *            The species class according to which the characteristics may
     *            change
     */
    public abstract String[] characteristics(E animalSpecies);

}

Now we implement them as :-

Code:
/**
* @author psychocoder
* @param <E>
*
*/
public class TestSkeleton<E> implements  Skeleton<E> {

    @Override
    public int noOfBones(E animalSpecies) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int averageHeight(E animalSpecies) {

        return 0;
    }

    @Override
    public int averageWeight(E animalSpecies) {

        return 0;
    }

    @Override
    public String[] characteristics(E animalSpecies) {

        return null;
    }    
}

Now implement the methods or write the codes or functionality you want them to provide.

TestSkeleton<String> species = new TestSkeleton()<>;


Here's the code to run the above interface

Code:
/**
* @author psychocoder
*
*/
public class Animal {    
    
    public static void main(String...strings){
        String animalSpecies = "Mammels";
        TestSkeleton<String> species = new TestSkeleton<>();
        System.out.println(species.averageHeight(animalSpecies));
        System.out.println(species.averageWeight(animalSpecies));
        System.out.println(species.noOfBones(animalSpecies));
        for (String s : species.characteristics(animalSpecies)){
            System.out.println(s);
        }        
    }
}

But lets forget about the passing of parameters E for now as you must read the generics to get a better conception of them. For now we consider that we have all the data to be passed as String. So we will only consider the first interface with strings.

Here is the complete example :-

Skeleton.java

Code:
public interface Skeleton {

    /**
     * @return the number of bone present in the body
     */
    public abstract int noOfBones(String animalSpecies);

    /**
     * @return the average height of the the animalSpecies
     */
    public abstract int averageHeight(String animalSpecies);

    /**
     * @return the average weight of the the animalSpecies
     */
    public abstract int averageWeight(String animalSpecies);

    /**
     * @return an array of the characteristics of the animalSpecies
     * @param animalSpecies
     *            The species class according to which the characteristics may
     *            change
     */
    public abstract String[] characteristics(String animalSpecies);

}

TestSkeleton.java

Code:
/**
* @author psychocoder
* @param <E>
*
*/
public class TestSkeleton implements Skeleton {

    @Override
    public int noOfBones(String animalSpecies) {
        switch (animalSpecies.toLowerCase()) {

        case "mammels":
            return 216;
        case "aquatic":
            return 216;
        case "aerial":
            return 216;
        default:
            return -1;
        }

    }

    @Override
    public int averageHeight(String animalSpecies) {

        switch (animalSpecies.toLowerCase()) {

        case "mammels":
            return 145;
        case "aquatic":
            return 300;
        case "aerial":
            return 50;
        default:
            return -1;
        }
    }

    @Override
    public int averageWeight(String animalSpecies) {
        switch (animalSpecies.toLowerCase()) {

        case "mammels":
            return 80;
        case "aquatic":
            return 296;
        case "aerial":
            return 21;
        default:
            return -1;
        }
    }

    @Override
    public String[] characteristics(String animalSpecies) {
        String[] m = new String[2];

        switch (animalSpecies.toLowerCase()) {

        case "mammels":
            m[0] = "Intelligent";
            m[1] = "Sexy";
            return m;
        case "aquatic":
            m[0] = "We rule the mighty ocean";
            m[1] = "We CUM :P";
            return m;
        case "aerial":
            m[0] = "We rule the sky";
            m[1] = "We chill";
            return m;
        default:
            return null;
        }
    }
}

Animal.java

Code:
/**
* @author psychocoder
*
*/
public class Animal {    
    
    public static void main(String...strings){
        String animalSpecies = "Mammels";
        TestSkeleton species = new TestSkeleton();
        System.out.println(species.averageHeight(animalSpecies));
        System.out.println(species.averageWeight(animalSpecies));
        System.out.println(species.noOfBones(animalSpecies));
        for (String s : species.characteristics(animalSpecies)){
            System.out.println(s);
        }        
    }
}

Here's a video tutorial : http://www.youtube.com/watch?v=XJWrYXv42Vw

I want you to read this :- http://answers.yahoo.com/question/index?...226AA8PnaB

and see this example :- http://java-buddy.blogspot.in/2013/02/im...using.html

I hope you had read this trail :- http://docs.oracle.com/javase/tutorial/j...index.html


I hope this was useful. A Simple Java Tip - Using interfaces can increase readability of your code, allow you to expand its functionality. Careful use makes your code shorter and a cleaner look.
[Image: OilyCostlyEwe.gif]

Reply

RE: Difference static and non static, public and private #32
@Deque @"Psycho_Coder" thank you very much i appreciate it i'm gonna read all this and see if i still have problems Smile
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Difference static and non static, public and private #33
@Deque @"Psycho_Coder" thank you very much i appreciate it i'm gonna read all this and see if i still have problems Smile
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Difference static and non static, public and private #34
@Deque @"Psycho_Coder" thank you very much i appreciate it i'm gonna read all this and see if i still have problems Smile
[Image: blackeagle_zps6ad86521.gif]

Reply







Users browsing this thread: 2 Guest(s)