Discussion Forum : Introduction To Methods And Streams
Question -


What is the output of this program?


class A {
int i;
public void display() {
System.out.println(i);
}
}
class B extends A {
int j;
public void display() {
System.out.println(j);
}
}
class Dynamic_dispatch {
public static void main(String args[])
{
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r = obj2;
r.display();
}
}
Options:
A .  1
B .  2
C .  3
D .  4
Answer: Option B

r is reference of type A, the program assigns a reference of object obj2 to r and uses that reference 

to call function display() of class B.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
2



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

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