This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question (...Inheritance) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question (...Inheritance)" Watch "Question (...Inheritance)" New topic
Author

Question (...Inheritance)

Chris Cairns
Ranch Hand

Joined: Jan 31, 2003
Posts: 48

C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called. Thus C is correct answer.
I don't understand why it matters whether the Parent's method is private or all of the above.
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! She also added a bit more descriptive title... ]
[ March 11, 2003: Message edited by: Jessica Sant ]
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203

I don't understand why it matters whether the Parent's method is private or all of the above

The explanation is very clear. Just read it 4 or 5 times and try to compile code with varous access specifiers. You will get the solution
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
A private method in a class is really just a 'helper method' - a way of organising the code efficiently within a class (maybe two or three methods within the class will want to use this method).
It has no meaning outside an instance of this class as, being private, it may not be called from any instance of any other class (including subclasses).
If method1() was not private in the superclass, it would be overriden in the subclass, so printing Child's method1() on execution.
Hope this helps -
Jon


SCJD, SCEA
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hello! In addition to Jon Entwistle's post...
If parent's method1() was declared with a different access modifier, the child's method1() will result to as a valid override method. And in such case, the method belonging to the object type (in the example above is Child) will be invoked.
Remember this rule:
** overload->refernece type (at compile time)
** override->object type (at runtime).


SCJP 1.4<br />SCWCD 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jon Entwistle:
If method1() was not private in the superclass, it would be overriden in the subclass, so printing Child's method1() on execution.

Just to state the same thing in a different way - private members are not inherited by subclasses. Therefore, the method is not overridden because the child class has no knowledge of the parent's method in the first place.
I hope taht helps.
Corey


SCJP Tipline, etc.
 
wood burning stoves
 
subject: Question (...Inheritance)
 
Similar Threads
Method Invocation
private vs. public
question about polymorphism
About Overridden II (a Mock Question question...)
clarification on why the modifier private changes output..