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 Sub class methods 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 "Sub class methods" Watch "Sub class methods" New topic

Sub class methods

preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93

Answer says 22 according to the exam .. how is that true.. I say the answer is 21
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 204
Hi Preeti,

At //Line 1 when u create an instance of Extension, the super class 0 argument constructor is invoked which invokes the add() method with value 1, because of dynamic binding the add() method of Extension is called and not the add() method of Base.

i+= 1*2 ;
Therefore the value of i=2;
The constructor of the Child class has an explicit call to add method with value 2. Hence the value of i becomes 6.
i += 2*2;
Therefore value of i = 6;
From the method bogo() the add() method is called with a value of 8.

i += 8*2;
Therefore value of i = 22;
when called the 'b.print()' method, it calls the method print() inherited by child 'Extension' and hence prints the value of 22.
Hope this clears your doubt
[ April 02, 2003: Message edited by: Reshma Pai ]

SCJP 1.4, SCWCD 1.4
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
thanks ... As usual I forgot about the dynamic binding call
I agree. Here's the link:
subject: Sub class methods
It's not a secret anymore!