Discussion Forum : Introduction To Methods And Streams
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 method_overriding {
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 method_overriding.java
$ java method_overriding
2



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

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