What will be printed after executing following program 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());
}
}
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());
}
}
A. 10
B. 20
C. 30
D. 40
E. None of these
Answer: Option D
Join The Discussion
Comments ( 8 )
What is method overriding in Java?
A. Redefining a superclass method in a subclass
B. Defining a new method with the same name in a subclass
C. Making a method private in a subclass
D. Hiding methods in a superclass
What is the purpose of method overloading in Java?
A. Creating static methods
B. Hiding methods in a superclass
C. Redefining methods in a subclass
D. Defining multiple methods with the same name but different parameters
A. @OverrideMethod
B. @OverrideSuper
C. @Override
D. @OverrideParent
What happens when a subclass tries to override a final method from the superclass in Java?
A. The final method is hidden
B. It results in a compilation error
C. The final method becomes static
D. The final method is hidden
what I think is....since the add function of the base class would be overridden,
whenever the base class constructor is called,value=value+20 would be executed and the same with child class constructor.
so
STEP1) base class constructor is called, so
value=0+20=>20
STEP2) child class constructor is called,so
value=20+20=>40
class Base{
int value = 0;
Base(){
addValue(); //call subclass addValue() method...value become 20
}
void addValue(){
value +=10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue(); //call his own method...already value=20
}
void addValue(){
value +=20; //value=20+20
}
}
public class Main{
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
how its print 40
Method is overridden and When Parent class reference variable refers to Child class object, any call to addvalue() will lead to call to the addvalue() present in Child class. Hence 20+20=40.
If you put 30 instead of 20, you will get 60 as output.
The method in the base class constructor ie addvalue() is overridden in subclass so on calling addvalue() in super class will actually call subclass addvalue() hence value become 20
When Parent class reference variable refers to Child class object, it is known as Upcasting
Correct Answer is 40.
Explanation
First it would called the parent class constructor which would add the value which is 0+10=10, so the current value is 10.
Second it would the child class constructor which called the addValue of the parent class 10+10=20. so the current value is 20.
Third it would called the parent addvalue method so the value is 20+20=40.
Since the example is method overriding , where the method name should be the same only the behaviour change
someone please explain this question
First constructor :
value = 0+ 10 = 10
second constructor :
value = 10 + 20 = 30
40 is Wrong !!!!! Dude...
if there is some other reason explain please.