+ 3

why System.out.println("Grr..."); //"Grr..." is not printed ? // but the class dog is extends animal can anyone explain m

class Animal { public void makeSound() { System.out.println("Grr..."); } } class Cat extends Animal { public void makeSound() { System.out.println("Meow"); } } class Dog extends Animal { public void makeSound() { System.out.println("Woof"); } } class Program { public static void main(String args[ ]) { Animal a = new Dog(); Animal b = new Cat(); a.makeSound(); b.makeSound(); } } why System.out.println("Grr..."); //"Grr..." is not printed ? // but the class dog is extends animal can anyone explain me and what if i want to print the line ?

20th Sep 2017, 11:11 AM
Prathamesh Dhuri
Prathamesh Dhuri - avatar
3 Respuestas
+ 4
Because the Animal's makeSound is overridden by the Dog's makeSound
20th Sep 2017, 11:15 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 4
thanks @~ swim ~ and @Baptiste E for guiding and helping me
21st Sep 2017, 5:09 AM
Prathamesh Dhuri
Prathamesh Dhuri - avatar
+ 2
Is Polymorphism and Overriding are same or different ?
21st Sep 2017, 5:05 AM
Prathamesh Dhuri
Prathamesh Dhuri - avatar