Java inheritance interview questions

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 its own properties along with the inherited properties from its super class.

 

How Inheritance can be implemented in java?

Inheritance can be implemented in JAVA using below two keywords:

1.extends

2.implements

extends is used for developing inheritance between two classes and two interfaces.

implements keyword is used to developed inheritance between interface and class.

 

What are the types of inheritance?

There are 5 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.)

 

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

 

No, a class in java cannot 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.

 

You know that all classes in java are inherited from java.lang.Object class. Are interfaces also inherited from Object class.?

 

No, only classes in java are inherited from Object class. Interfaces in java are not inherited from Object class. But, classes which implement interfaces are inherited from Object class.

 

How do you restrict a member of a class from inheriting to its sub classes?

By declaring that member as a private. Because, private members are not inherited to sub classes.

 

Can a class extend itself?

No, a class cannot extend itself.

 

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.

 

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.

 

Are static members inherited to sub classes?

Yes, Static members are also inherited to sub classes.

 

Why we need to use Inheritance?

 

  1. For Code Re usability.
  2. for Method Overriding.

 

What is syntax of inheritance?

 

public class subclass extends superclass{

 

//all methods and variables declare here

}

 

What is multilevel inheritance?

Getting the properties from one class object to another class object level wise with different priorities.

 

Are constructors inherited? Can a subclass call the parent’s class constructor? When?

 

You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of its superclasses. One of the main reasons is because you probably don’t want to override the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.

 

Can we reduce the visibility of the inherited or overridden method?

No.

 

Which of the following is tightly bound? Inheritance or Composition?

Inheritance.

 

Does a class inherit the constructor of its super class?

No.

 

What are the difference between composition and inheritance in Java?

Composition – has-a relationship between classes.

Inheritance – is-a relationship between classes.

Composition – Composing object holds a reference to composing classes and hence relationship is loosely bound.

Inheritance – Derived object carries the base class definition in itself and hence its tightly bound.

Composition – Used in Dependency Injection

Inheritance – Used in Runtime Polymorphism

 

Composition – Single class objects can be composed within multiple classes.

Inheritance – Single class can only inherit 1 Class.

 

Composition – It’s the relationship between objects.

Inheritance – It’s the relationship between classes.