Knowee
Questions
Features
Study Tools

elect the correct answerWhat will be the output of the following Java program? class parent { int a; public void display() { System.out.println(a); } } class child extends parent { int b; public void display() { System.out.println(b); } } class Dynamic_dispatch { public static void main(String args[]) { child obj2 = new child(); obj2.a = 2; obj2.b = 4; parent r; r = obj2; r.display(); } }Options6428

Question

elect the correct answerWhat will be the output of the following Java program? class parent { int a; public void display() { System.out.println(a); } } class child extends parent { int b; public void display() { System.out.println(b); } } class Dynamic_dispatch { public static void main(String args[]) { child obj2 = new child(); obj2.a = 2; obj2.b = 4; parent r; r = obj2; r.display(); } }Options6428

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

Solution

The output of the program will be 4.

Here's the step by step explanation:

  1. A child object obj2 is created and a and b are assigned values 2 and 4 respectively.

  2. A parent reference r is created and assigned the obj2 object.

  3. When r.display() is called, the display() method of the child class is executed because of dynamic method dispatch, which is a mechanism by which a call to an overridden method is resolved at runtime rather than compile-time.

  4. In the display() method of the child class, b is printed, which is 4.

So, the output is 4.

This problem has been solved

Similar Questions

Select the correct answerWhat will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options102Compilation Error

the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java program? class parent { int a; } class child extends parent { int b; void display() { super.a = b + 1; System.out.println(b + " " + i); } } class inheritance { public static void main(String args[]) { child obj = new child(); obj.a=1; obj.b=2; obj.display(); }}

Observe the below code :class Parent  {      public void display() {                System.out.println("Parent");      }}class Child extends Parent  {      public void display() {                System.out.println("Child");      }}public class Main  {       public static void main(String args[ ])   {               Parent p =   ;               Child  c = ;               p.display();  c.display();       }}Fill the code accordingly, so that you get the output asChildChild.

elect the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java code? class parent {  int x;     void display() {      System.out.println(x);     } }     class child extends parent {     int y;     void display() {         System.out.println(y);     } }     class method_overriding {     public static void main(String args[]) {          child obj = new child();         obj.x=4;         obj.y=5;            obj.display();          } }Options354Compilation Error

Select the correct answerWhat is the output of the following java program?class Parent { public final void show() {    System.out.println("Parent::show() called");  }}class Child extends Parent {  public void show() {      System.out.println("Child::show() called");  }}public class Main {  public static void main(String[] args) {    Parent p = new Child();;    p.show();  }}OptionsCompiler ErrorBase::show() calledDerived::show() calledException

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.