+ 2

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)"); } }

10th Feb 2020, 12:32 AM
Gino ^^
Gino ^^ - avatar
4 Respuestas
+ 2
Your example is bleeding from syntax errors, and I advise it is always better to save all your code in the playground and link it to the question, instead of pasting in the question description. You seem to have 5 test cases and 6 outputs so that doesn't really add up. Your examples relate to runtime polimorphism and late binding. https://www.sitepoint.com/quick-guide-to-polymorphism-in-java/ When you create a subclass object with the type of its ancestor, it will still have all the characteristics of the subclass, including the overridden methods. Animal pigeon = new Pigeon() ; This will certainly make a pigeon sound. Creating objects like this, is useful because you can include them in arrays and collections that share the same type, like Animal[] It is good practice to use the @Override notation on the subclass methods that change the parent class behavior. Check the fixed code again https://code.sololearn.com/cKvGAWC015hR/?ref=app
10th Feb 2020, 6:01 AM
Tibor Santa
Tibor Santa - avatar
+ 2
zemiak you are right... But we can use downcasting when we have to access members that belong only to the subclass. Animal pigeon = new Pigeon(); Pigeon p = (Pigeon) pigeon; p.pigeonOnlyMethod(); https://www.codejava.net/java-core/the-java-language/what-is-upcasting-and-downcasting-in-java
10th Feb 2020, 7:15 AM
Tibor Santa
Tibor Santa - avatar
+ 1
> "When you create a subclass object with the type of its ancestor, it will still have all the characteristics of the subclass" It is true in this example, because the subclass has no extra method or property compared to the superclass
10th Feb 2020, 6:43 AM
zemiak
0
Pigeon doesn't print "(Bird sound)" output is: Animal bird = new Bird(); // (Bird Sound) Animal pigeon = new Pigeon(); // (Pigeon Sound) Bird pigeon2 = new Pigeon(); // (Pigeon Sound) Human dane1 = new Dane(); // (Dane Sound) Animal dane2 = new Dane(); // (Dane Sound)
10th Feb 2020, 6:17 AM
zemiak