Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance example  RSS feed

 
Sarika Chavan
Greenhorn
Posts: 21
  • 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: 1090
14
  • 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: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • 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
  • 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).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!