What will be the output of the following Java 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();
}
}
a) 1
b) 2
c) 3
d) 4
Answer: b
Explanation: 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.
Related Posts
Which of these constructors is used to create an empty String object?
Which of these method of class String is used to extract a single character from a String object?
Which of this method of class String is used to obtain a length of String object?
Which of these operators can be used to concatenate two or more String objects?
Which of these class is superclass of String and StringBuffer class?
Does Java support multiple level inheritance?
What would be the result if a class extends two interfaces and both have a method with same name and signature? Lets assume that the class is not implementing that method.
Join The Discussion