Java Interview Questions - Inheritance

Here are the most popular Java interview questions about inheritance.

See all questions

Is super class instantiated when subclass is instantiated?


No, the object created is an instance of both the super class and subclass, but there’s no separate instance of the super class. In order to create a separate instance of the super class it must be instantiated directly.

The constructor of the super class is always called when the subclass is instantiated, although which constructor of the super c

...

Can a base-type access properties in its sub-types?


When a class is inherited, the base type can have properties shared by all sub-types, and each sub-type can have their specific properties. The sub-type properties cannot be accessed by other sub-types of the same Generic object or even in the base-type.

What is method overloading and method overriding?


Method overloading is having several forms of the same method, each differing in the formal argument list.

Method overriding is when the exact same method definition is created in a sub-class, which results in hiding the base-class method.

What is super? Are the following uses of super correct?


The super keyword is used to reference the parent class and methods of the parent class. super can be used to invoke the constructor of the parent class as well. In this case, it must be the first line of code. If there is no default constructor in the base class, then it is mandatory to call one of the base class parameterized constructors.

...

If a super class method is throwing RuntimeException, can it be overriden in a subclass method throwing NullPointerException?


In Java, it is possible to override a method with a different throws signature if the base class method only throws Runtime Exception (unchecked exceptions). If the base class method throws checked exceptions, then the subclass method has to have one of the following:

Looking for developers?