File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another method invocation question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another method invocation question..." Watch "Another method invocation question..." New topic

Another method invocation question...

Cliff DeRose

Joined: Jan 08, 2004
Posts: 13

I understand that by extending Parent, Child inherits "method2()". When the method is called in main("p.method2()"), I understand that at run-time it is determined by the JVM that the underlying object which p refers to (of type Child) has its "method2()" called. Standard polymorphic call.
Once the inherited "method2()" starts executing, and the call to "method1()" is made, what I don't understand is why "method1()" from the Parent class is used instead of "method1()" from the Child class. Can anybody shed some light on why the result of this code was the following:
Parent's method2()
Parent's method1()
I was expecting this:
Parent's method2
Child's method1
[ January 09, 2004: Message edited by: Cliff DeRose ]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Private methods are not inherited and are not overridden. The method1 in the child class does not override the method1 in the parent class.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Cliff DeRose

Joined: Jan 08, 2004
Posts: 13
So method1() in the Parent class and method1() in the Child class are two totally distinct things as viewed by the compiler? Is that correct? method1() in the Child class would be like making a new method specific only to the Child class - it has no relation at all to the Parent class?
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
That's right there is no relation at all between the two methods because a private method cannot be overridden...
I agree. Here's the link:
subject: Another method invocation question...
It's not a secret anymore!