Knowee
Questions
Features
Study Tools

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.pr  10203040

Question

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.pr  10203040

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the program will be 40.

Here's the step by step explanation:

  1. When Base b = new Derived(); is executed, the Derived class constructor is called. But before that, the Base class constructor is called (as per Java's constructor chaining).

  2. In the Base class constructor, the addValue() method is called. Since this is an overridden method in the Derived class, the addValue() of Derived class is called (this is called upcasting in Java).

  3. In the addValue() method of Derived class, the value is increased by 20. So, value becomes 20.

  4. After the Base class constructor is finished, the Derived class constructor is executed. Again, the addValue() method of Derived class is called and value is increased by 20. So, value becomes 40.

  5. Finally, System.out.println(b.getValue()); prints the value which is 40.

This problem has been solved

Similar Questions

What will be the output of the following code?This question is required.*class Base {public void display() {System.out.println("Base");}}class Derived extends Base {public void display() {System.out.println("Derived");}}public class Main {public static void main(String[] args) {Base b = new Derived();b.display();}}

What will be the output?class A{ int i = 10; public void printValue(){ System.out.print("Value-A"); }}class B extends A{ int i = 12; public void printValue(){ System.out.print("Value-B"); }}public class Test{ public static void main(String args[]){ A a = new B(); a.printValue(); System.out.print(a.i); }}Value-B 11Value-B 10Value-A 10 Value-A 11

Select the correct answerWhat will be the output of the following Java program? final class base     {         int a;    }        class derived extends base    {        int b;        System.out.println(b + " " + a);      }        class inheritance     {        public static void main(String args[])        {            derived obj = new derived();            obj.display();             }   }OptionsRuntime Error3 32 2Compilation Error

class Base { public void Print() { System.out.println("Base"); }} class Derived extends Base { public void Print() { System.out.println("Derived"); }} class InhDriver { public static void printMsg(Base ob) { ob.Print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); printMsg(x); printMsg(y); printMsg(z); }}

correct answerWhat will be the output of the following Java code? class main {    public int x;    protected int y; }     class derived extends main {     int y;     void display() {         super.y = 5;         System.out.println(x + " " + y);     } }     class Output {     public static void main(String args[]) {          derived obj = new derived();         obj.x=2;         obj.y=3;            obj.display();          } }Options2 32 53 25 2

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.