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 can anyone explain me the output..?? 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 "can anyone explain me the output..??" Watch "can anyone explain me the output..??" New topic
Author

can anyone explain me the output..??

rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Mock exam on http://java.about.com/library/quizzes/blscjp-002.htm

output --
Parent's method2()
Parent's method1()


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

the fact is u r not overridding the method1() since u declared it as private in the super class.when u r not overriding a method , it willbe invoked by the reference not by the objects.
method1() in subclass is totally new to that class.
so, when u r callig that method it will call the super class method.
Parent p = new Child();
p.method2();
take my lock test : www.geocities.com/krishna4java/index.html


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Tony Jackson
Ranch Hand

Joined: Feb 23, 2001
Posts: 45
Clearly the parent's method2() is being called (since the Child doesn't have a method2) but inside that method is the call of method1(). Since no object is used, the current instance (ie, "this") is used. If you print out the type of "this" with:
Object o = this;
System.out.println("this is object of type: " + o.getClass().getName());
Then it prints out it is of type "Child", not parent. So then why isn't the Child's method1() called? Krishna S., you said that it was because the Parent's method is private. Why would that be enough to call it instead of calling the child's method? Since the object is a Child, shouldn't the Child's method be called?
(I did try this out, and the behavior does match with what you said it would, but I don't understand it.)
Tony
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
okay Tony
i'll try to explain the issue here...
think this way,
1. Parent has two methods, method1 and method2 and method2 is calling method1 which is private
2. this means method2 has a pointer to method1 in Parent
3. child also has method1 BUT here method1 is "private" in Parent hence doesn't get "inherited" to child AND THATS WHY child has two copies of the method1 (one from the parent and one child wrote).
4. this implies that parent's method2 is still pointing to Parent's method1 (as method1 didn get overridden)
BUT if u make method1 public in Parent then child inherits that method and as child also re-defines method1 it "overrides" the method1 and so method2 in Parent will call method1 in child instead of parent.
u get that?
if i try to put it in some dummy real scenario,
1. consider person C and his/her parent P where both of them are business man
2. P offers one service called "deal with government for some matter" (public method) and helds private practices (preparing paper work etc) to complete the public service
3. C decides to offer "preparation of papers" as service (public method) BUT also announces that he CAN "deal with government for some matter" as he can delegate the service to his parent P (and client never knows about that trick). Here, C doesn't know that what P does to perform public service of dealing with goverment (of course the business secret)
4. Now, when client calls C's service that is dealing with the government, C will call P and requests him to do the service
Now, at this step what do we expect? do we expect P to use C's service for preparing paper work and all for performing the "dealing with government"? NO. why? because P might be doing something which "he only knows" and he can't delegate it to C because C's methodology is publicly known...you see...so P still uses "his private" techniques to perform the service rather than asking C to do it...
i don't mean to point to any ethical issues here but i just tried to come up with an example..

regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anyone explain me the output..??