Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java polymorphism basics

Tags:

java

I am reading the SCJP book by Kathy Sierra. I find Polymorphism a little confusing.

Could you please help me out with a real world example for the following?

I understand that Polymorphism only works when you have overridden methods, no matter if you do it via class or interface, and at run-time the JVM determines the method based on the Object type.

Lets say Horse extends from Animal and it also overrides the eat() method. What is the benefit of doing: Animal a = new Horse(); a.eat(); over Horse b = new Horse(); b.eat();?

Eventually the result is going to be the same. I apologize its a very basic question but even all the senior developers in my team gave me all different answers.

like image 642
t0mcat Avatar asked Mar 31 '11 18:03

t0mcat


4 Answers

To extend your example, say you have a farm of animals. Then a collection of all animals could be defined as

List<Animal> animals = new ArrayList<Animals>();
animals.add(new Horse());
animals.add(new Cow());
animals.add(new Sheep());

Then you could iterate over all the animals and have them all eat by

for(Animal a: animals) {
    a.eat();
}

Also they're essential for Design Patterns. I recommend you read about them @ http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

like image 130
Kurru Avatar answered Sep 21 '22 07:09

Kurru


Imagine you have class Animal and three implementations : Horse, Cow and a Pig. And then you have a field where they are walking. So, you can make the following:

List<Animal> animals = new ArrayList<Animal>(3);
animals.add(new Horse());
animals.add(new Cow());
animals.add(new Pig());

for (Animal animal : animals) {
  animal.eat();
}

So here in the cycle you don't mind what concreate animal do you have: it can be a pig, or a cow, or the full list can be filled with just horses. But they are all animals, and they all can eat. Polymorphism is great here, because independent of what animal do you have, the method eat will be chosen in dependcy of the concreate object.

like image 43
Vladimir Ivanov Avatar answered Sep 19 '22 07:09

Vladimir Ivanov


Lets say you have a farm.

List<Animal> farm = new ArrayList<Animal>();
farm.add(new Horse());
farm.add(new Pig());
farm.add(new Chicken());

Now lets make them all eat.

for(Animal animal : farm){
    animal.eat();
}
like image 37
Jeremy Avatar answered Sep 22 '22 07:09

Jeremy


If you just have one subclass you can't explicitly see the advantages, but suppose you have also a second one called Lion:

public abstract class Animal {
   public abstract void eat;
}

public class Horse extends Animal {

   public void eat() {
      System.out.println("Eats gras");
   }

}

public class Lion extends Animal {

   public void eat() {
      System.out.println("Eats meat");
   }

}

And now you have a list of animals you can just do this and it will print to correct eat:

List<Animal> animals = new List<Animal>();
animals.add(new Lion());
animals.add(new Horse());

for(Animal a: animals) {
    a.eat();
}
like image 25
RoflcoptrException Avatar answered Sep 21 '22 07:09

RoflcoptrException