It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt-Khalid Mock Test,Q.No 14 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 "Doubt-Khalid Mock Test,Q.No 14" Watch "Doubt-Khalid Mock Test,Q.No 14" New topic
Author

Doubt-Khalid Mock Test,Q.No 14

arswami
Greenhorn

Joined: Jul 01, 2000
Posts: 22
Hi Everybody,
I'm quite new to this..
Kindly clear my below mentioned doubt.
What are we doing at line marked 1.Are we creating an object?if yes,where is the reference for the object?Do we not require reference?
This program is compiling and the number 22 gets printed.
Thanks in advance,
Swaminathan.
_____
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()); //Line 1
}
static void bogo(Base b){
b.add(8);
b.print();
}
}


Ken Lai
Ranch Hand

Joined: Aug 06, 2000
Posts: 30
new Extension()
is an anonymous object created within the parameter of the bogo() method . The reference is b within the bogo method. Yes you can create an object without giving it a reference. Try this:
<code>
class Test
{
public static void main(String[] args)
{
new Test().print();
}

void print()
{System.out.println("Method invoked");}
}
</code>
This is the same as:

<code>
class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.print();
}

void print()
{System.out.println("Method invoked");}
}
</code>
hope this helps.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
HI:
would you please give me some detail explanation how the result is 22. Thanks.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
reference can be created directly is right, but can anybody explain how the answer is 22. IMO it should be 17.
Satish Varanasi
Ranch Hand

Joined: Aug 13, 2000
Posts: 47
Originally posted by emily:
HI:
would you please give me some detail explanation how the result is 22. Thanks.

emily,
First main method of class Qd073 gets executed which calls the static method bogo().The bogo() method has an argument new Extension() that is creating an object of class Extension,thus calling the constructor of class Extension.
In the constructor Extension() , there is a call to add() method passing an argument 2,before that the constructor of base class is called i.e Base()
Now when all the superclass constructors are called then the member variable i in the base class get initialized to default value 0.then the add() method in the base class is called,but the method which gets executed is of the Extension class(here is the catch)because the object is of Extension class making the value of i = 2.
Then the control passes to add(2) method of the Extension class thus the value of i=6.
Now the add(8) method gets executed thus i=22;

need clarification more the ! then do ask
bye
satish_varanasi
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Varanasi:

Thanks very much for your help. Now I am clear.
Emily
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The answer is indeed 22.
Following is the flow of events.
1. new Base()
-----This calls the add() method of Extension (not Base as you thought)
so i+=v*2 ======> i = i + (1 * 2) ====> i=2
2. new Extension()
-----This calls the add() method Extension
so i+=v*2 ======> i = i + (2*2) =====> i=6
3. bogo() calls add() method of Extension
so i+=v*2 ======> i = i + (8 * 2) ====> i=22

arswami
Greenhorn

Joined: Jul 01, 2000
Posts: 22
Thanks Ken and all,
I'm clear now.
Swaminathan.
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
Can somebody pls expalin why it calls the add() method of Extension (not Base as I thought). Is it overriding.
Can the constructor of superclass( Base here) call an overiding method from subclass (Extension here).
Thanks.
Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
Yeah Harry, it is due to overridding. IMO, the base class just calls the method add(). It doesn't mention anything about whether the base copy or the subclass copy should be called (and it can't). Anyway, at runtime, when add() is called, the class of the object is checked which turns out to be Extension, so the add method associated with Extension is called. It doesn't have to do anything from the fact that the method is being called from the Base class.
uh..oh..I think the explanation has got a li'll messy
Thanks,
Aman
Frank Wang
Greenhorn

Joined: Aug 18, 2000
Posts: 25
This is dynamic binding. when Extension() is called, Base() is implicitly called first. But since an object of Extension is being created, the add(i) is dynamicly binded with void add(int v) in class Extension. After this, everything goes fine.
 
GeeCON Prague 2014
 
subject: Doubt-Khalid Mock Test,Q.No 14