• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from Khalid's mock test

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>
What will be the result of the following code?
1. class Base {
2. int i;
3. Base(){
4. add(1);
5. }
6. void add(int v){
7. i+=v;
8.
9. }
10. void print(){
11. System.out.println(i);
12. }
13. }
14.
15. class Extension extends Base{
16. Extension(){
17. add(2);
18. }
19. void add(int v){
20. i+=v*2;
21.
22. }
23. }
24.
25. public class Test6{
26.
27. public static void main(String[] args){
28. bogo(new Extension());
29.
30. }
31. static void bogo(Base b){
32. b.add(8);
33. b.print();
34.
35. }
}
</code>
I dont remember the answers but I compiled and found the answer to be 22. I may have some fundamental doubt here since I would think that
a. line#28 would cause the variable i to have a value 1 (see line#7) when it is constructed via an implicit call to the default constructor of the Base class but instead it is having a value 2. (I printed out the value of i at line #8 in a separate program).
b. line#28 would cause the variable i to have a value 5
after i+=v*2; (see line#20) when the Extension class is constructed but instead it is having a value 6. (I printed out the value of i at line #8 in a separate program).
c. line#32 would cause the variable i to have a value 21
after i+=v*2; (see line#20) when it will call the b.add(8) in line#32 but instead it is having a value 22. (I printed out the value of i at line #8 in a separate program).
My question is . If I have a method :
void aMethod(int v){
i+=v;
} where i is a class member variable int i.
Does the passing of an argument b.aMethod(1) change the value of both i and v to 1? If so, why line#32 does not cause b.add(8) to have the result 24 instead of 22 i.e (i = 8 + 8*2) whereas line #4 add(1) and line#17 add(2) gives result (i=1+1=2) and (i=2+2*2=6) respectively.
I have no other doubts regarding latebinding etc. I hope I could be clear enough.
[This message has been edited by Howard Stern (edited April 25, 2000).]
[This message has been edited by Howard Stern (edited April 26, 2000).]
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard,
not 100% sure about this, but I think that when the default call to super() is made in the Extension constructor, it will still use the overridden add() method in the Extension class. So the add(1) in the Base constructor will yield two. Then the total is 22. Perhaps someone could confirm that.
Also, in your final example code did you mean to say void aMethod(int v) because otherwise i don't follow you.
Eric
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard,
The Execution flow is like this.
1. - Line 28 called
- new Extension()
- add(2) called
- the overriden version of add in Extension called since we are
creating an object of the subclass Extension
- so i+=v*2; which is i= i +(v*2)
so i = 0+4=4


2. - This created Extension object in step 1 is passed to as a Base object
to bogo(Base b)
- this calls b.add(8).
- Since add is instance method which is overiden by Extension class
and the physical object created in heap in step 1 above is an
Extension object, NOT Base object, even now inside the bogo(Base b)
method, the overriden version of the add(..) in Extension is called

- Since the instance var i is already changed its default value from
0 to 4 (as in step 1) now this b.add(8) in step 2 further
continues changing that.
- so i = i+(v*2)
i = 4 +(8*2)
i = 22;


3. So b.print() is the inherited print() and not overiden in Extension which just prints the i value which is 22
If you have any more doubts please reply back.
regds
maha anna
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have two qns.
1)When you create the Extension object won't it call the Base constructor??
2)Your result seems to be wrong.
4+(8*2)=20 not 22.Have you missed something??
My approach.
1)When Extension object is about to be created, Extension constructor is called.
Since Extension extends Base, super() is called. Since we are actually creating object of Extension, add in Extension is called.(add(1)).Next add(2) in Extension constructor is reached.Now i is 6(2+2*2)
2)bogo is called .
add(8) will give
6+8*2=22

[This message has been edited by Mani (edited April 25, 2000).]
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You are right Mani. See how silly I was . I didn't notice even in the addition part.
regds
maha anna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. I had never come across any theoritical explanation of the above mentioned problem anywhere. Did anybody come across an explanation in any book where the late binding of methods during object initialization is explained?
In any case the explanation does make sense to me! Thanks a lot.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I post my idea here for this question.
The key point is:
Java compiler rewrites the code segment below
16. Extension() {
17. add(2);
18. }
into
16. Extension() {
16'. super(); //Base(){add(1);}
17. add(2);
18. }
Then, we can explain the result.
hzhang
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example seen above, if Base() is called when Extension is instantiated, then add(1) would result in 1, since
int i is not intialised in Base it has 0;
then add(2) would yield :1+2*2=5;
then b.add(8) would be 5+8*2=21.
Can anybody explain it to me. The answer when I compile is 22.
The explanation above was not clear to me how i=2;
Call me stupid but I need to know..
Thanks
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smithani,
From your post
--------------
(1) add(1) would result in 1, since int i is not intialised in Base it has 0;
(2) then add(2) would yield :1+2*2=5;
(3) then b.add(8) would be 5+8*2=21.
You missed only one point. Since you are creating an Extension object (not Base object), since the Extension class overrides the add(int i) from the Base class , when Base() is called, add(1) is called which in turn calls the overriding add(int i)method in Extension is called and NOT base class version
So add(1) will give (Extension version of add )
<pre>

add(int v) {
i += v*2; // i = i+ (v*2) //i = 0 +(1*2) // i=2;
}

</pre>

So now you get the result as 22 instead of 21. You overlooked the overriding part I think
regds
maha anna
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smithani,
I have just written a C++ version of the program and it prints out 21. In C++, an object is not completely created before the constructor is successfully executed. So the add(1) function will be Base.add(int v) in C++.
However, in Java an object is created immediately after the new operator is executed. If the object is created, it can invoke the add(int v) method polymorphically.
Here is the disassembled code for the Base constructor:
Method Base()
0 aload_0
1 invokespecial #7 <Method java.lang.Object()>
4 aload_0
5 iconst_1
6 invokevirtual #8 <Method void add(int)>
9 return
Address 1 creates the Base object (a subclass of java.lang.Object).
Address 5 pushes 1 to the stack. Address 6 then calls a virutal function (call it overloaded function in Java) of add(int). Because the Extension obj is already created in calling bogo(new Extension()), the Extension.add(int) is called.
Here is the disassembled code for the Extension constructor.
Method Extension()
0 aload_0
1 invokespecial #4 <Method Base()>
4 aload_0
5 iconst_2
6 invokevirtual #5 <Method void add(int)>
9 return

Therefore i += 1*2 (value of i is 2) while you are still in Base ();
After the constructor Base() completes, it returns back to constructor Extension. In Extension(), add(2) is called. Now i += 2*2 (value of i is 6).
You end up with 22.
 
Edward Man
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for some missing code due to my ignorance of HTML code.
Here they are:
Method Base()
0 aload_0
1 invokespecial #7 < Method java.lang.Object() >
4 aload_0
5 iconst_1
6 invokevirtual #8 < Method void add(int) >
9 return
Method Extension()
0 aload_0
1 invokespecial #4 < Method Base() >
4 aload_0
5 iconst_2
6 invokevirtual #5 < Method void add(int) >
9 return
Edward
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maha Anna, I understand it now.Thanks for taking the time to reply to both of my posts on the same issue.
Edward Man thanks to you for taking the trouble of explaining it to me.
I really appreciate both of your help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic