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" .
After interchanging the given two numbers and two signs what will be the values of equation (I) and (II) respectively?
× and +, 3 and 9
Which of the following is an example of .xls and.doc?
The ratio of two numbers is 7 : 5, and their HCF is 6. What is their LCM?
किस संख्या का 18% संख्या 75 के 12% के बराबर है ?
दर्पण में देखी गई घड़ी सवा तीन बजे का समय दिखाती है। घड़ी में...
A 150-litre solution of juice and water contains the two in the ratio 3:2, respectively. If 30% of the solution is replaced with water, what will be the...
Which of the following statement is correct?
In each question below is given a statement followed by two conclusions numbered I and II. You have to assume everything in the statement to be true, t...
- A number '71945b' is divisible by 6, but not by 5. What is the value of (b² + 1)?
मेरी बहन मुझसे 352 दिन बड़ी है जबकि मेरा भाई उससे 65 सप्ताह बड़ा ...