Trying to understand polymorphism
While trying to understand how polymorphism works in Java, I wrote this little test class that has this code in its main method: Animal bird = new Animal pigeon = new Pigeon(); Bird pigeon2 = new Pigeon(); Human dane1 = new Dane(); Animal dane2 = new Dane(); bird.makeSound(); pigeon.makeSound(); pidgeon2.makeSound(); dane1.makeSound(); dane2.makeSound(); My question is why the output is: (Bird Sound) (Bird Sound) (Human Sound) (Bird Sound) (Dane Sound) (Dane Sound) If the Pigeon method prints (Bird Sound), then why doesn't the Dane method print (Human)? Thanks for your help :) The respective classes are: public class Human extends Animal { public void makeSound() { System.out.println("(Human Sound)"); } } package polymorphism; public class Bird extends Animal { public void makeSound() { System.out.println("(Bird Sound)"); } } package polymorphism; public class Pidgeon extends Bird { public void makeSound() { System.out.println("(Pidgeon Sound)"); } } package polymorphism; public class Animal { public void makeSound() { System.out.println("(Standard Animal Sound)"); } } and package polymorphism; public class Dane extends Human { public void makeSound() { System.out.println("(Dane Sound)"); } }