which one will it invoke? base or derived?
Forum » SCJP2 Discussion / inheritance » which one will it invoke? base or derived?
Started by: itsraghzitsraghz
On: 1201200238|%e %b %Y, %H:%M %Z|agohover
Number of posts: 2
rss icon RSS: New posts
Summary:
will it invoke the method in base class or derived class? also the order/timings of initialization of members in each class..
which one will it invoke? base or derived?
itsraghzitsraghz 1201200238|%e %b %Y, %H:%M %Z|agohover

Question by Jose Campana:
Analyze this case and please tell me Why the method with the comment //This method is never reached.

class Base{
  int value = 0;
  Base(){
     addValue();
  }
  void addValue(){ //This method
     value += 10;
  }
  int getValue(){
     return value;
  }
}
class Derived extends Base{
 
  Derived(){
     addValue();
  }
  void addValue(){
    value +=  20;
  }
}
public class Test3 {
  public static void main(String[] args){
     Base b = new Derived();
     System.out.println(b.getValue());
  }
}

The preceding code prints out 40. And of course, that means that the method addValue() in class Base() never executes.

When inside Base() constructor, it calls the addValue() method, and it uses the one defined in class Derived. but the Question is:

WHY ?

unfold which one will it invoke? base or derived? by itsraghzitsraghz, 1201200238|%e %b %Y, %H:%M %Z|agohover
Re: which one will it invoke? base or derived?
itsraghzitsraghz 1201201591|%e %b %Y, %H:%M %Z|agohover

[[toc]]

Reply from Fred Rosenberger, sheriff, author:

the object being created is a Derived.

Reply from Bill Shirley:

during the magic of new, the java runtime calls the constructors of the superclass and the subclass.

You have overriden addValue() in the subclass. All instances of the subclass have this method replaced.

The only (non-reflective) way to get to it is to invoke super.addValue() from the subclass.

class Base{
  int value = 0;
  Base(){
     addValue();
  }
  void addValue(){ //This method overriden by subclass
     value += 10;
  }
  int getValue(){
     return value;
  }
}
class Derived extends Base{
 
  Derived(){
     addValue();
  }
  void addValue(){
    super.addValue();  
    value +=  20;
  }
}
public class Test3 {
  public static void main(String[] args){
     Base b = new Derived(); // b isa Derived
     System.out.println(b.getValue()); // output now 60
  }
}

Reply from Yelamuri Chandu:

You are creating a object of Drevied and assingn it to a variable with ref Base.

When you are creating any Object wiht new Keyword what the JVM will do is first it creates all the varaibles and methods belong to its super and then it own. At any point of time if you call any varible or method first it will try to pick from its own area if not available it goes to super area.

here also the addvalue is available with Derived one, so it executes the devried class method.

In this case if you want to supress and call super class methods then you need to use super keyword which invoke super class method directly.

unfold Re: which one will it invoke? base or derived? by itsraghzitsraghz, 1201201591|%e %b %Y, %H:%M %Z|agohover
New post
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.