my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pls help me out.. 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 "Pls help me out.. " Watch "Pls help me out.. " New topic
Author

Pls help me out..

Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi all..
pls have a look at the following code..
What will this program print out ?
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40
the correct answer is 40.. how is this so.. shouldn't the method in the base class constructor call its own addValue() method??
also, if the methods are declared static, the output is 30.. aren't static methods resolved by the type or reference and not by the type of object??
Thanx in advance..

------------------
Hima


Hima<BR>Sun Certified Java Programmer
mousami bhattacharya
Ranch Hand

Joined: May 13, 2001
Posts: 40
What will this program print out ?
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40

The answer 40 is correct ,what happens is
1. When new Derived() is called ,it calls the super() i.e Base()
2.Base() in turn calls the addValue() ,but due to dynamic overriding the addValue() of Derived is called ,Thus value=20.
3.Now the call returns from super and executes the next line in Derived constructor i.e addValue().This is again a call to the Derived classes addVallue() method ,so now value = 20+20 = 40

When you make addValue static.
1.When the super() call is made the addValue() of parent is called ,so value = 10.
2.when the call returns from super the addValue() line in the Derived() constr is executed ,i.e value = 10 + 20 = 30

Hope this helps
M :-)
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Hima,
Whenever you create an object by a reference of the superclass
the method calls decided by the "type of object" while the variables are referenced by the "type of reference". In the question that you have put. The sequence of events are as follows:
(1)<code> Base b = new Derived(); </code> . This implicitely calls the constructor of the base, by an implicite
<code>super()</code>
(2)<code> addValue(); </code> method of Derived is called so the value of variable "value" after <code>value += 20; </code> becomes 20 as it was initially "0".
(3) call to subclass constructor is invoked i.e., <code> Derived() </code> is invoked.
(4)<code> addValue(); </code> method of Derived is called agin so the value of variable "value" after <code>value += 20; </code> becomes "40" as it was incremented to "20" in the (2) above.
The final value stored in variable "value" is "40".
Hope this clears your doubt.
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 14, 2001).]
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
hi,
the method in the base class constructor will call the method in the derived class because it is overriden (run time binding).
but if u make the methods static then the methods are not overriden, its called method hiding and the method call gets resolved at compile time depending on the type of the object reference.
shanks.
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Originally posted by Hima Mangal:
hi all..
pls have a look at the following code..
What will this program print out ?
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40
the correct answer is 40.. how is this so.. shouldn't the method in the base class constructor call its own addValue() method??
also, if the methods are declared static, the output is 30.. aren't static methods resolved by the type or reference and not by the type of object??
Thanx in advance..

Now let's walk through the execution of the code and see if that would help answer your question.
1 - In main(), Derived object is instantiated via 'new' op
2 - This in turn will invoke Derived's constructor
3 - Derived's constructor will implicitlyinvoke super() (i.e. invoking Base's constructor)
4 - Since Base is top-level class, its field will be intialized next
5 - int value will be assigned with 0
6 - call addValue() in Base's constructor (note: due to overriding - obj is Derived, this method is actually Derived's)
7 - field value goes from 0 to 20 via execution of value += 20
8 - Done with addValue() and back in Base's constructor
8 - Return from Base's constructor back to Derived's constructor
9 - call addValue() in Derived's constructor
10 - field value goes from 20 to 40 via execution of value += 20
11 - Done wirth addValue() and back in Derived's constructor
12 - return to main()
13 - Assign the created object reference to type Base named b
14 - call System.out.println(), which call getValue() first
15 -getValue() returns field value, which was set to be 40
16 - System.out.println() outputs the value fo 40
If both addValue() is declared with static access, then whichever class where addValue() is called, the method belongs to that class unless it is qualified by another name. So at step #6, Base's addValue() is called and the field value will go from 0 to 10 via value += 10; All the remaining execution steps remain and at step #10 field value will go from 10 to 30...
Hope that helps,
- Lam -
Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi all..
thanx a lot for clearing my doubt.. now i understand why the answer was so..
thanx again to all of u..

------------------
Hima
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pls help me out..