Skip to content
C

Java Interview Questions

Inheritance Interview Questions

Inheritance, the super keyword, method hiding, and the diamond problem.

Question 1: What is inheritance in Java?

Ans

Inheritance lets one class (the subclass/child) reuse and extend the fields and methods of another class (the superclass/parent) using the extends keyword. It's appropriate when there's a genuine "is-a" relationship and shared behavior naturally belongs in a common ancestor.

Example

java
class Animal { void eat() { System.out.println("Eating"); } } class Dog extends Animal { void bark() { System.out.println("Bark"); } }

Important Point

Java supports only single inheritance for classes (one direct parent), though a class can implement multiple interfaces.

Question 2: What types of inheritance does Java support, and why doesn't it support multiple inheritance of classes?

Ans

Java directly supports single inheritance (one parent), multilevel inheritance (a chain: grandparent, parent, child), and hierarchical inheritance (several children sharing one parent). It deliberately does not allow a class to extend two classes at once, because if both parents defined a conflicting field or method implementation, there would be no unambiguous way to decide which one the child inherits — the "diamond problem." Interfaces sidestep this because, before default methods, they carried no state and no conflicting implementation.

Important Point

Java achieves the flexibility of "multiple inheritance of type" safely through interfaces, just not multiple inheritance of class implementation.

Question 3: What is the super keyword, and what are its main uses?

Ans

super refers to the immediate parent part of the current object. It has three common uses: calling a parent constructor (super(...), must be the first statement), accessing a parent field that's been hidden by a same-named child field, and calling a parent's version of a method that the child has overridden.

Example

java
class Dog extends Animal { Dog() { super(); } @Override void eat() { super.eat(); System.out.println("Dog eats too"); } }

Important Point

super() must be the first statement in a constructor when used explicitly, exactly like this().

Question 4: What is final?

Ans

final prevents further change depending on where it is used. A final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended.

It is useful when a value or design contract should not be changed by later code.

Example

java
final int MAX = 100;

Important Point

A final reference cannot point to another object, but the referenced object's internal state may still be mutable.

Continue Your Preparation