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
Solution
The output of the program will be 4.
Here's the step by step explanation:
-
A
childobjectobj2is created andaandbare assigned values 2 and 4 respectively. -
A
parentreferenceris created and assigned theobj2object. -
When
r.display()is called, thedisplay()method of thechildclass 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. -
In the
display()method of thechildclass,bis printed, which is 4.
So, the output is 4.
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
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.