Prerequisite: Inheritance in Java
1) What is the output of the following program?
Output: fa la
Explanation: B b = new A(); b is object of type B, and hence b.sing() refers to the method sing of class B see run-time polymorphism
2) What is the output of the following program?
Geek-Buiding Geek-House Geek-house: String ConstructorGeek
Explanation: Constructors call their superclass default constructor, which execute first, and that constructors can be overloaded.
First House constructor with one argument is called and flow shifts to no-arg constructor of house class due to this(). From here, due to superclass default constructor, no-arg constructor of building is called. Hence the order shown. For detail See – Constructors in Java
3) What is the output of the following program?
Output: Compiler Error
Explanation: Final methods cannot be overridden. For Detail see final keyword. However, if we remove the keyword final then, the output will be
4) What is the output of the following program?
Output: Compiler error
Explanation: exception ArithmeticException has already been caught is shown. Terminal
Ordering of catch blocks is important The More specific/subclass (ArithmeticException) need to come earlier and more general/superclass (Exception) need to be written later. The program will execute correctly if the order of Arithmetic exception and general exception is interchanged.
5) What is the output of the following program?
Output: block -ve2e made -ve2e
Explanation: The static initialization block is the only place where an instance of Echo is created. Then the Echo instance is created, Echos no-arg constructor calls its 1-arg constructor, which then calls Vibrates constructor (which then secretly calls Objects constructor). At that point, the various constructors execute, starting with Objects constructor and working back down to Echos no-arg constructor. see static keyword
Quiz on Inheritance
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.