Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Inheritance strange behaviour

The following code prints output as 40 instead of 30. I'm not able to figure out the solution. Please help with the code.

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());
  }
}

  The implicit super reference in Derived constructor calls Base constructor which in turn calls method addValue() in the class Base results in value variable as 10 and then addValue() in the Derived class should add 20 to value 10. So the final output is 30.

But the code prints 40.

like image 369
Rakesh Avatar asked Oct 12 '18 13:10

Rakesh


People also ask

Do subclasses inherit variables Java?

A subclass also inherits variables and methods from its superclass's superclass, and so on up the inheritance tree.

Which method is called if there are overridden methods in an inheritance hierarchy?

In that case, the method in the subclass overrides the method in the superclass and effectively replaces its implementation, as shown in Figure 6-3. Overriding methods to change the behavior of objects is called subtype polymorphism.

Can we inherit subclass?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Can a parent inherit from a child Java?

Similarly, in Java, when some properties or behaviors get passed from parent class to the child class, it is said that child class inherits from a parent class. Child classes can have properties and methods of its own as well. They can also override the behaviors (methods) of the parent class.


Video Answer


4 Answers

The addValue method is overridden in the Derived class. When a method is overridden, calling a method on an instance of this class always calls the overridden version, even when the call happens in the base class.

like image 103
yole Avatar answered Oct 16 '22 20:10

yole


In class Derived, the method void addValue() points to the method defined in Derived, not in Base

like image 4
ControlAltDel Avatar answered Oct 16 '22 21:10

ControlAltDel


Most probably when you extend the Base class

class Derived extends Base {

  Derived() {

    addValue();

  }

  void addValue() { //here

    value +=  20;

  }

}

you put the method name same in the Base Class and this overrides the default one which is:

   void addValue() {
      value += 10;
    }

So, the output is 40 -> 20 + 20

like image 2
Rarblack Avatar answered Oct 16 '22 19:10

Rarblack


As others have got there before me: addValue is overridden in Derived, as it is an accessible method with the same name and same parameter types. Typically you would add the @Override annotation to the override method. The method in the derived class even though the base class is still under construction.

Not all languages do the same thing. C++, for example, will not run overridden methods from derived class whilst the base constructor is still running. The equivalent program in C++ does show 30.

#include <iostream>

class Base {
public:
    int value;

    Base() : value(0) {
      addValue();
    }

    virtual void addValue() {
      value += 10;
    }

    int getValue() {
      return value;
    }
};

class Derived : public Base {
public:
  Derived() {
    addValue();
  }

  virtual void addValue() {
    value +=  20;
  }
};

int main() {
    Base *b = new Derived();
    std::cout << b->getValue() << std::endl;
}
like image 1
Tom Hawtin - tackline Avatar answered Oct 16 '22 19:10

Tom Hawtin - tackline