Discussion Forum : Introduction To Methods And Streams
Question -


What is the output of this program?


class A {
public void display() {
System.out.println("A");
}
}
class B extends A {
public void display() {
System.out.println("B");
}
}
class Dynamic_dispatch {
public static void main(String args[])
{
A obj1 = new A();
B obj2 = new B();
A r;
r = obj1;
r.display();
r = obj2;
r.display();
}
}
Options:
A .  A B
B .  B A
C .  Runtime Error
D .  Compilation Error
Answer: Option A

Call to display function of class A and class B is made by using dynamic method dispatch, by 

using this method a call to an overridden function is resolved at run time, rather than at compilation time.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
A B



Was this answer helpful ?
Next Question
Submit Your Solution hear:

Your email address will not be published. Required fields are marked *