Java | Class and Object | Question 5
Predict the output of the following program.
class First { void display() { System.out.println("Inside First"); } } class Second extends First { void display() { System.out.println("Inside Second"); } } class Test { public static void main(String[] args) { First obj1 = new First(); Second obj2 = new Second(); First ref; ref = obj1; ref.display(); ref = obj2; ref.display(); } } |
chevron_right
filter_none
(A) Compilation error
(B)
Inside First Inside Second
(C)
Inside First Inside First
(D) Runtime error
Answer: (B)
Explanation: ‘ref’ is a reference variable which obtains the reference of object of class First and calls its function display().
Then ‘ref’ refers to object of class Second and calls its function display().
Quiz of this Question
Recommended Posts:
- Java | Class and Object | Question 2
- Java | Class and Object | Question 4
- Java | Class and Object | Question 7
- Java | Class and Object | Question 1
- Java | Class and Object | Question 4
- Java | Class and Object | Question 6
- Object class in Java
- C++ | Class and Object | Question 6
- C++ | Class and Object | Question 4
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 2
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 5
- Object level and Class level locks in Java
- Java | Abstract Class and Interface | Question 1



