Sail E0 Webinar
Question


What is the output of this 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();
}
}
Options:
A .  0
B .  1
C .  2
D .  Compilation Error
Answer: Option C

class A & class B both contain display() method, class B inherits class A, when display() method 

is called by object of class B, display() method of class B is executed rather than that of Class A.
output:
$ javac inheritance_demo.java
$ java inheritance_demo
2



Was this answer helpful ?
Next Question

Submit Solution

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

Latest Videos

Latest Test Papers