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.
A subclass also inherits variables and methods from its superclass's superclass, and so on up the inheritance tree.
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.
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.
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.
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.
In class Derived
, the method void addValue()
points to the method defined in Derived, not in Base
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With