Question
What will be the output of the following Java snippet?
class A { Â Â Â public void display() { Â Â Â Â Â Â Â System.out.println("Class A"); Â Â Â }} class B extends A { Â Â Â public void display() { Â Â Â Â Â Â Â System.out.println("Class B"); Â Â Â }} public class Test { Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â A obj = new B(); Â Â Â Â Â Â Â obj.display(); Â Â Â }}Solution
This program demonstrates runtime polymorphism in Java through method overriding. When the method display() is called on the object obj, the overridden method in class B is executed. This is because obj is instantiated as new B(), and at runtime, the JVM determines the appropriate method to execute based on the actual type of the object. Thus, even though obj is declared as type A, the overridden display() method in B is executed, producing the output "Class B" . This behavior is a key feature of Java's dynamic method dispatch mechanism. Explanation of Incorrect Options: A) Class A : This would be true if the display() method in class B was not overridden. However, since B overrides A's method, this option is incorrect. C) Compilation Error : The code is syntactically correct, so it compiles without any issues. D) Runtime Error : The program runs successfully, as all method calls are valid and properly resolved at runtime. E) None of the above : This is incorrect, as the correct output is explicitly "Class B" .
Which of the following is the first sentence of the passage?
Rearrange the following sentences in the proper sequence to form a meaningful paragraph and then answer the questions given below.
A. These bea...
In the questions given below, a sentence has been divided into four fragments (A), (B), (C) and (D) and is arranged (not necessarily in the correct ord...
Given that statement 1 is the first sentence, which of the following will be the SIXTH sentence after rearrangement?
Which of the following is the first sentence of the passage?
In Each question below has a sentence with four highlighted words that are jumbled. You need to rearrange them in the correct order to make a coherent s...
Directions: In each of the following questions, a sentence is given with three words marked as (A), (B), and (C). These words may or may not be placed ...
The downturn (1) resulted in a market capitalization (2) loss of nearly Rs 3 lakh crore for all BSE-listed companies(3) , as volatile (4)surged amid pr...
Which of the following is the fifth sentence of the paragraph after rearrangement?
1. He equipped himself with a long, sturdy stick, and then started on his eighteen-kilometer expedition back to his village through the swollen flood w...