aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance example" Watch "Inheritance example" New topic
Author

Inheritance example

Sarika Chavan
Greenhorn

Joined: Feb 14, 2014
Posts: 21
Hello, I am confused with following code.



Options:
1. parent
2. child

Why the answer is parent..not child.

Please help me.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
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

Joined: Sep 26, 2006
Posts: 9495
    
    2

The same example when tried in Scala has the following output:



SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Sarika Chavan
Greenhorn

Joined: Feb 14, 2014
Posts: 21
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
 
subject: Inheritance example