• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance example

 
Sarika Chavan
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am confused with following code.



Options:
1. parent
2. child

Why the answer is parent..not child.

Please help me.
 
Chan Ag
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Coderanch, Sarika.

The void say() method is only in the Parent class. It knows nothing about the message variable in the Child class. Yes, the child class has inherited it but it is still printing the message variable it knows about, i.e one defined in the Parent class.
Is there a reason why you think it should print child?
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same example when tried in Scala has the following output:

 
Sarika Chavan
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:Welcome to Coderanch, Sarika.

The void say() method is only in the Parent class. It knows nothing about the message variable in the Child class. Yes, the child class has inherited it but it is still printing the message variable it knows about, i.e one defined in the Parent class.
Is there a reason why you think it should print child?


Thanks for your help.
I thought since child class is inheriting the method, it is the one which executes method with its variable(i.e. message).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic