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
The database environment has all of the following components except:
Related fields in a database are grouped to form a
A logical schema
The language used in application programs to request data from the DBMS is referred to as the
NULL is
In case of entity integrity, the primary key may be
In an E-R diagram attributes are represented by
Join The Discussion