It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in inheritance" Watch "Doubt in inheritance" New topic
Author

Doubt in inheritance

suja changanam
Greenhorn

Joined: Sep 27, 2007
Posts: 12
Hi
Source of this code is Khalid mogal's exam simulator,

The code is given below,and the output shows 22,I am not able to understand how it comes 22.


class Base {
int i;
Base() {
add(1);
}
void add(int v)
{
i += v;
}
void print()
{
System.out.println(i);
}
}
class Extension extends Base {
Extension()
{
add(2);
}
void add(int v)
{
i += v*2;

}
}
public class Qd073 {
public static void main(String[] args)
{
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}

Thanks in advance
suja
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38
1. Line 30 creates an instance of Extension.
2. The compiler would have inserted an no-arg call to super() as the first line of the Extension constructor, and so we evaluate Base.add(1), which sets the instance variable i to 1.
3. Returning to the Extension constructor, we call Extension.add(2), which sets i = i + 2*2, or 6.
4. So now we have an instance of Extension, with an attribute of i with a value of 6. We pass this object to Qd073.bogo(), as type Base, and call Base.add(8). But since b is actually of type Extension, through polymorphism we get the version of add() in Extension, and so i is set to i + 8*2, or 22.


SCJP 5, SCWCD 5
Klemens Katterbauer
Greenhorn

Joined: Jul 15, 2008
Posts: 8
Hi Mike,

I think you've miscalculated a little bit, because since when is 1+ 2*2 = 6.

Shouldn't that be 5.

I think I know why things going that way.

Look at the following code:



It prints out 0

So the logic of the program is simple,

An new Extension-object is created, the super call to the base constructor then invokes the add method of the extension class id. i == 0+ 2*1=2
then the Extension constructor calls add and therefore i == 6 and so
we get 22, when one performs the last step.


What strucks me a little is that this might mean that when you have a super call from a subclass constructor and both constructors have the same invocation of a method, that's overridden in the subclass, then both constructors invoke the method of the subclass.
Explanation?


Klemens

[ August 01, 2008: Message edited by: Klemens Katterbauer ]

[ August 01, 2008: Message edited by: Klemens Katterbauer ]
[ August 01, 2008: Message edited by: Klemens Katterbauer ]

Klemens Katterbauer<br />Student of Mathematics,<br />University of Vienna
gobburi saikrishna
Ranch Hand

Joined: Jan 21, 2008
Posts: 52
i think answer is 21, can anyone please explain how answer is 22

Thanks
saikrishna
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38

I think you've miscalculated a little bit, because since when is 1+ 2*2 = 6.


Quite right. Sorry bout that.


What strucks me a little is that this might mean that when you have a super call from a subclass constructor and both constructors have the same invocation of a method, that's overridden in the subclass, then both constructors invoke the method of the subclass.


I think Klemens has it exactly right:

Meena Ajay
Ranch Hand

Joined: May 28, 2008
Posts: 38

Hi,
Trying to trace the flow of the code,
1.When new Extension() is called, an implicit Super() call is inserted in the Extension constructor before calling add(2).
2.Inside the Base() constructor, value of i is 0.Now , add(1) is called with an implicit this object,which is actually calls the Extension class' add method.
3.So, now value of i becomes i +=v*2 , i.e., i =0 + (1*2) , which is i=2
4.After executing Super call, the add(2), inside the Extension() constructor is executed.
5.Now value of i after executing i +=v*2 will become 6 i.e., i = 2 +(2*2)
6.The call to bogo inside main takes the Extension object , now value of i being 6. bogo in turn calls add(8), which invokes extension class' add method.
7.Now i +=8*2 , i.e., i = 6 +(8*2) = 6+16 =22

Hope this helps.


Cheers, Meena
OCPJP 6
suja changanam
Greenhorn

Joined: Sep 27, 2007
Posts: 12
Thanks to all, now I got it.

Suja
 
wood burning stoves
 
subject: Doubt in inheritance
 
Similar Threads
Another question
Constructors & methods
mughal question,need help
Khalid Mock - Constructor Evaluation
Constructor Problem