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 #21
(10-25-2013, 10:33 AM)blackeagle Wrote: @Deque as i understood public variables could be accessed in any class
and the private can only be accessed by using the methods get and set
am i right ?

Yes.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Difference static and non static, public and private #22
(10-25-2013, 10:33 AM)blackeagle Wrote: @Deque as i understood public variables could be accessed in any class
and the private can only be accessed by using the methods get and set
am i right ?

Yes.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Difference static and non static, public and private #23
@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
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Difference static and non static, public and private #24
@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
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Difference static and non static, public and private #25
@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
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Difference static and non static, public and private #26
Imagine you write a program that does something with dogs. In this case with a sheperd dog.

Code:
public class DogProgram {
    
    public static void main(String[] args) {
        SheperdDog dog = new SheperdDog("Rex");
        careFor(dog);
    }

    public static void careFor(Sheperd dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

Code:
public class SheperdDog {

    private String name;

    public SheperdDog(String name) {
        this.name = name;
    }

    public eat(DogFood food) {
        System.out.println(name + " is eating dog food");
    }

}

Now you also want to take care for other dogs, like @bluedog.tar.gz
In order to do that you would make it the naive way like this (without interface):

Code:
public class DogProgram {
    
    public static void main(String[] args) {
        SheperdDog sheperd = new SheperdDog("Rex");
        careFor(sheperd);
        BlueDog bluedog = new BlueDog();
        careFor(bluedog);
    }

    public static void careFor(SheperdDog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

    public static void careFor(BlueDog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

Code:
public class BlueDog {

    private String name = "bluedog.tar.gz";

    public void eat(DogFood food) {
        System.out.println("I don't want to eat dog food, give me some real stuff");
    }

}

Now imagine you add even more dogs. For every new dog type you would have to create a new careFor() method, because they are different classes. But essentially you are doing the same, you feed all of them dog food. By creating a Dog interface you can take care of this.

Code:
public interface Dog {

    public void eat(DogFood food);

}

Now your DogProgram only needs one careFor() method for all of them:

Code:
class DogProgram {
    
    public static void main(String[] args) {
        Dog sheperd = new SheperdDog("Rex");
        careFor(sheperd);
        Dog bluedog = new BlueDog();
        careFor(bluedog);
    }

    public static void careFor(Dog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

An interface will also tell other programmers how to implement a new dog type so it works with your program.

Also have a look at common Java interfaces, i.e. the List interface. There are two standard lists: The ArrayList and the LinkedList. Both have the same interface, because a lot of programs do not care which kind of list it is as long as they can use them the same way.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Difference static and non static, public and private #27
Imagine you write a program that does something with dogs. In this case with a sheperd dog.

Code:
public class DogProgram {
    
    public static void main(String[] args) {
        SheperdDog dog = new SheperdDog("Rex");
        careFor(dog);
    }

    public static void careFor(Sheperd dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

Code:
public class SheperdDog {

    private String name;

    public SheperdDog(String name) {
        this.name = name;
    }

    public eat(DogFood food) {
        System.out.println(name + " is eating dog food");
    }

}

Now you also want to take care for other dogs, like @bluedog.tar.gz
In order to do that you would make it the naive way like this (without interface):

Code:
public class DogProgram {
    
    public static void main(String[] args) {
        SheperdDog sheperd = new SheperdDog("Rex");
        careFor(sheperd);
        BlueDog bluedog = new BlueDog();
        careFor(bluedog);
    }

    public static void careFor(SheperdDog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

    public static void careFor(BlueDog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

Code:
public class BlueDog {

    private String name = "bluedog.tar.gz";

    public void eat(DogFood food) {
        System.out.println("I don't want to eat dog food, give me some real stuff");
    }

}

Now imagine you add even more dogs. For every new dog type you would have to create a new careFor() method, because they are different classes. But essentially you are doing the same, you feed all of them dog food. By creating a Dog interface you can take care of this.

Code:
public interface Dog {

    public void eat(DogFood food);

}

Now your DogProgram only needs one careFor() method for all of them:

Code:
class DogProgram {
    
    public static void main(String[] args) {
        Dog sheperd = new SheperdDog("Rex");
        careFor(sheperd);
        Dog bluedog = new BlueDog();
        careFor(bluedog);
    }

    public static void careFor(Dog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

An interface will also tell other programmers how to implement a new dog type so it works with your program.

Also have a look at common Java interfaces, i.e. the List interface. There are two standard lists: The ArrayList and the LinkedList. Both have the same interface, because a lot of programs do not care which kind of list it is as long as they can use them the same way.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Difference static and non static, public and private #28
Imagine you write a program that does something with dogs. In this case with a sheperd dog.

Code:
public class DogProgram {
    
    public static void main(String[] args) {
        SheperdDog dog = new SheperdDog("Rex");
        careFor(dog);
    }

    public static void careFor(Sheperd dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

Code:
public class SheperdDog {

    private String name;

    public SheperdDog(String name) {
        this.name = name;
    }

    public eat(DogFood food) {
        System.out.println(name + " is eating dog food");
    }

}

Now you also want to take care for other dogs, like @bluedog.tar.gz
In order to do that you would make it the naive way like this (without interface):

Code:
public class DogProgram {
    
    public static void main(String[] args) {
        SheperdDog sheperd = new SheperdDog("Rex");
        careFor(sheperd);
        BlueDog bluedog = new BlueDog();
        careFor(bluedog);
    }

    public static void careFor(SheperdDog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

    public static void careFor(BlueDog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

Code:
public class BlueDog {

    private String name = "bluedog.tar.gz";

    public void eat(DogFood food) {
        System.out.println("I don't want to eat dog food, give me some real stuff");
    }

}

Now imagine you add even more dogs. For every new dog type you would have to create a new careFor() method, because they are different classes. But essentially you are doing the same, you feed all of them dog food. By creating a Dog interface you can take care of this.

Code:
public interface Dog {

    public void eat(DogFood food);

}

Now your DogProgram only needs one careFor() method for all of them:

Code:
class DogProgram {
    
    public static void main(String[] args) {
        Dog sheperd = new SheperdDog("Rex");
        careFor(sheperd);
        Dog bluedog = new BlueDog();
        careFor(bluedog);
    }

    public static void careFor(Dog dog) {
        DogFood food = new DogFood();
        dog.eat(food);
    }

}

An interface will also tell other programmers how to implement a new dog type so it works with your program.

Also have a look at common Java interfaces, i.e. the List interface. There are two standard lists: The ArrayList and the LinkedList. Both have the same interface, because a lot of programs do not care which kind of list it is as long as they can use them the same way.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Difference static and non static, public and private #29
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 #30
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







Users browsing this thread: 3 Guest(s)