1

I have a decent understanding about polymorphism but my question is for example, in the code I posted below, what is so special about being able to make an object of Cat while using an animal type for the variable. What purpose does that serve? Also assume that the makenoise method is being override in each objects own class and that the class all the other classes are inheriting from is the animal class.

public class Demo
{
    public static void main(String[] args) {
        Animal a1 = new Cat(); //prints meow
        a1.makeNoise(); 

        Animal a2 = new Dog();
        a2.makeNoise(); //Prints Bark
    }
}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Ike Nwaogu
  • 21
  • 2
  • possible duplicate of [Benefit of Polymorphism](http://stackoverflow.com/questions/11082640/benefit-of-polymorphism) – jdphenix Sep 17 '14 at 19:26
  • Closely related: [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1065197) – Luiggi Mendoza Sep 17 '14 at 19:28

1 Answers1

2

Polymorphism provides a way to separate low-level details from the main flow of the program, by restricting the details to subclasses, while the program only knows about the higher-level classes. Polymorphism lets the program work in terms of generalizations.

To extend your example a little we'd have

abstract class Animal {
    public abstract void makeNoise();
}

class Cat extends Animal {
    public void makeNoise() { System.out.println("meow");}
}

class Dog extends Animal {
    public void makeNoise() { System.out.println("woof");}
}

class Demo {
    public static void main(String[] args) {
        List<Animal> animals = new ArrayList<Animal>();
        animals.add(new Cat());
        animals.add(new Dog());
        for (Animal animal: animals) {
            animal.makeNoise();
        }
    }
}

Here the Demo program can run through the animals list without making any special cases for which animal is a dog and which one is a cat. That means I can add new subclasses of Animal and the for-loop contents never have to change.

If it wasn't for polymorphism the code would look like

class Dog {
    public void bark() {System.out.println("woof");}
}

class Cat {
    public void meow() {System.out.println("meow");}
}

class Demo {
    public static void main(String[] args) {
        ArrayList<Object> animals = new ArrayList<Object>();
        animals.add(new Cat());
        animals.add(new Dog());
        for (Object animal: animals) {
            if (animal instanceof Dog) {
                ((Dog)animal).bark();
            } else if (animal instanceof Cat) {
                ((Cat)animal).meow();
            }
        }
    }
}

and now there is no commonality about the animal classes that the program can make use of. Without any useful generalization to use Demo has to know a lot more about the details. Every new kind of animal introduced will necessitate an addition to the for-loop body.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276