• 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

Pls help me out..

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 40
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 117
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all..
thanx a lot for clearing my doubt.. now i understand why the answer was so..
thanx again to all of u..

------------------
Hima
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic