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 statements about Subrahmanyam Jaishankar is/are correct?
(1) He was the then India’s ambassador to United State of Ameri...
Who was the first Odissi dancer from Odisha to receive Padma Vibhushan?
 Japan's 2+2 dialogue with India involves which two Indian ministries?
What is the maximum compensation that can be awarded by the Banking Ombudsman under the Reserve Bank of India's scheme?
Which of the following is correct in regards to the major schemes and their respective fund allocations for the fiscal year 2024-2025, as outlined in th...
Which institution are mandated to open at least one RSETI in their lead district to provide training to rural youth to take up self-employment/ entrepr...
Which of the following is NOT a Small Finance Bank (SFB)?
The nodal coordinating agency for the Project ‘PARI’ of the Ministry of Culture is
Which of the following won the 'NATIONAL TECHNOLOGY AWARD (MAIN)' in National Technology Week 2023?
Who was the writer of ‘Ain-i-Akbari’?