Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance question  RSS feed

 
Cairo Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




May I know why the result is



instead of "Animal say Hello" ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

See How my dog learned polymorphism.
 
Cairo Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, marc weber.

I can undertsand the article.

If myDog.sayHello() results "Dog says Hello", I can understand the theory behind.

But my doubt is a new Dog instance is created, and it implicitly got "super();" in its constructor, then inside Animal class constructor, it calls sayHello method. Why and how this superclass (Animal) knows to call its subclass (Dog)'s method, sayHello()? and print out "Dog says Hello" and not "Animal say Hello"?

In other words, how to write so in the same case, the Animal call it's own sayHello() method? I have tried to use "this.sayHello()" but it also print out "Dog says Hello".

Perhaps somewhere in my brain get stuck ~ sorry... but please tell me why.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cairo cairo:
... Why and how this superclass (Animal) knows to call its subclass (Dog)'s method, sayHello()? and print out "Dog says Hello" and not "Animal say Hello"? ...

This is polymorphism. The method sayHello() is defined in Animal, but it's also overridden in Dog. Because the true runtime type of the instance is Dog, the method body in Dog is invoked. Note that this would happen even if the reference type was Animal, for example...

Animal myAnimal = new Dog();
myAnimal.sayHello(); //Output: "Dog says hello."

One way to avoid this dynamic binding (associating the method body with the runtime type) is to make the methods static in both the super and subclass. This way, the method is not overridden, and is invoked based on the reference type instead of the runtime type. Another way is to make the method private in the superclass so that it's not inherited. You can also prevent overriding by making the method final in the superclass.

Note that within Dog, you can invoke the Animal version of sayHello() by calling super.sayHello().
 
Cairo Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the true runtime type of the instance is Dog


Yes, I got what you mean~ Thanks for your explanation. Thank you very much.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "cairo cairo"-

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!