Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt-Khalid Mock Test,Q.No 14

 
arswami
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI:
would you please give me some detail explanation how the result is 22. Thanks.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Varanasi:

Thanks very much for your help. Now I am clear.
Emily
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ken and all,
I'm clear now.
Swaminathan.
 
Harry Chawla
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic