Prerequisite – Inheritance in Java
Predict the output of following Java Programs.
Program 1 :
Output: Compilation fails
3: error: invalid method declaration; return type required public Top(String s) ^ 1 error
Explanation: The implied super() call in B’s constructor cannot be satisfied because there isn’t a no-arg constructor in A. A default, no-arg constructor is generated by the compiler only if the class has no constructor defined explicitly.For detail See – Constructors in Java
Program 2 :
Explanation: Although a final method cannot be overridden, in this case, the method is private, and therefore hidden. The effect is that a new, accessible, method flipper is created. Therefore, no polymorphism occurs in this example, the method invoked is simply that of the child class, and no error occurs.
Program 3 :
Output: alpha subsub
Explanation: SubSubAlpha extends Alpha! Since the code doesnt attempt to make a SubAlpha,
the private constructor in SubAlpha is okay.
Program 4 :
Output: No output is produced.
Explanation: In main(), the start() method was never called to start ”t” and ”t2”, so run() never ran.
For detail: See Multithreading in Java
Program 5 :
Output: Compiler Error in super.super.Print()
Explanation: In Java, it is not allowed to do super.super. We can only access Grandparent’s members using Parent. See Inheritance in Java
Related Article: Quiz on Inheritance in Java
This article is contributed by Pavan Gopal Rayapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Output of Java program | Set 23 (Inheritance)
- Output of Java Program | Set 4
- Output of Java Program | Set 7
- Output of Java Program | Set 8
- Output of Java program | Set 27
- Output of Java Program | Set 2
- Output of Java Program | Set 3
- Output of Java program | Set 26
- Output of Java Program | Set 11
- Output of Java Program | Set 19
- Output of Java Program | Set 9
- Output of Java program | Set 5
- Output of Java program | Set 17
- Output of Java program | Set 28
- Output of Java Program | Set 6