Inheritance Interview Questions Part II

What do you mean by inheritance?

Inheritance is one of the key features of object oriented programming. Through inheritance, a class (Sub Class) can inherit properties of another class (Super Class). Sub class can have it’s own properties along with the inherited properties from it’s super class.

What are the types of inheritance?
  1. Single Inheritance :

    One class is extended by only one class.

  2. Multilevel Inheritance :

    One class is extended by a class and that class in turn is extended by another class thus forming a chain of inheritance.

  3. Hierarchical Inheritance :

    One class is extended by many classes.

  4. Hybrid Inheritance :

    It is a combination of above types of inheritance.

  5. Multiple Inheritance :

    One class extends more than one classes. (Java does not support multiple inheritance.)

Learn in Detail about Java Programming

What is covariant return type?

Co-variant return type states that return type of overriding method can be subtype of the return type declared in method of superclass. it has been introduced since jdk 1.5

How compiler handles the exceptions in overriding ?
  1. The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.

  2. If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception.

  3. Different exceptions in java follow some hierarchy tree(inheritance). In this case, if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method.

Can a class extend more than one classes or does java support multiple inheritance? If not, Why?

No, a class in java can not extend more than one classes or java does not support multiple inheritance. To avoid ambiguity, complexity and confusion, java does not supports multiple inheritance.

For example, If Class C extends Class A and Class B which have a method with same name, then Class C will have two methods with same name. This causes ambiguity and confusion for which method to use. To avoid this, java does not supports multiple inheritance.

Can a class extend itself?

No, A class can not extend itself.

Which of these is correct way of inheriting class A by class B?

Options :

  1. class B + class A { }

  2. class B inherits class A { }

  3. class B extends A { }

  4. class B extends class { }

Answer : c.

Explanation : None.

Are constructors and initializers also inherited to sub classes?

No, Constructors and initializers(Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.

How do you implement multiple inheritance in java?

Through interfaces, we can implement multiple inheritance in java. As classes in java can not extend more than one classes, but a class can implement more than one interfaces.

What happens if both, super class and sub class, have a field with same name?

Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.

Learn more about Inheritance

Are static members inherited to sub classes?

Yes, Static members are also inherited to sub classes.

Which of the following statements are incorrect?

Options :

  1. public members of class can be accessed by any code in the program.

  2. private members of class can only be accessed by other members of the class.

  3. private members of class can be inherited by a sub class, and become protected members in sub class.

  4. protected members of a class can be inherited by a sub class, and become private members of the sub class.

Answer : c.

Explanation : private members of a class cannot be inherited by a sub class.