This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Justify the output of the code (inheritance/overriding problem) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Justify the output of the code (inheritance/overriding problem)" Watch "Justify the output of the code (inheritance/overriding problem)" New topic
Author

Justify the output of the code (inheritance/overriding problem)

pranav thakker
Greenhorn

Joined: Nov 21, 2005
Posts: 20
Hello ranchers,

Can anyone justify the output of following code ?



According to myself output should be as follow :-

Parent's method2()
Child's method1()

Because, we are calling method2 with reference of child class.

But, the actual output is :-


Parent's method2()
Parent's method1()

Please, explain it.

Thanking you all,

Pranav Thakker
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
Output

Parent's method2()
Parent's method1()

is absolutely correct.

method1 cannot be invoked in polymorphic way because you do not override it. As long as it is marked private it cannot be inherited.
If you would change it to public your output will be as you thought.


SCJP 5.0 (91%), SCBCD 1.3 (94%), SCWCD 1.4 (88%)
lavanya sankuappan
Greenhorn

Joined: Jun 02, 2006
Posts: 17
private method,method1() in parent class is not overridden by child class.it is another method with the same name
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
If u change the method1() in parents class like:

then the output would be
Parents method2()
Child method1()


Thanks
Balaji.S
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144

But, the actual output is :-
Parent's method2()
Parent's method1()


It's a Child object and a Child declaration, so how is it possible that the output above has "Parent's method1()"?!!
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Search JavaRanch and you can find more posts about this.
Here is one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Justify the output of the code (inheritance/overriding problem)