Examveda
Examveda

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

A. 10

B. 20

C. 30

D. 40

E. None of these

Answer: Option D


This Question Belongs to Java Program >> Overriding And Overloading

Join The Discussion

Comments ( 8 )

  1. P Abhijith
    P Abhijith :
    6 years ago

    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

  2. Ajit Ranpise
    Ajit Ranpise :
    6 years ago

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

  3. Aarthi Viswanathan
    Aarthi Viswanathan :
    7 years ago

    how its print 40

  4. Mukul Agrawal
    Mukul Agrawal :
    7 years ago

    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.

  5. Sumesh K
    Sumesh K :
    7 years ago

    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

  6. Kaushal Shah
    Kaushal Shah :
    8 years ago

    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

  7. Harshit Chauhan
    Harshit Chauhan :
    8 years ago

    someone please explain this question

  8. Sunny Bhan
    Sunny Bhan :
    8 years ago

    First constructor :
    value = 0+ 10 = 10

    second constructor :
    value = 10 + 20 = 30

    40 is Wrong !!!!! Dude...
    if there is some other reason explain please.

Related Questions on Overriding and Overloading