Inheritance in Java: What Should You Know?

Inheritance in Java is a methodology by which a class gets the property of another class. Using it, a child inherits the detail from its parents. By the use of this property, a child class gets the properties of its parents. It means that with the help of inheritance, you can use the data and the functions of an existing class. It allows you to reuse a class which is an essential property in OOPs.

Various Types of Inheritance

There are many kinds of inheritance in Java, let’s take a look:

  • Single Inheritance: In it, one class extends another class. The parent class is called super-class, and that class that extends is called sub-class.
  • Multiple Inheritance: In it, one class extends more than one class, and it is not supported in Java.
  • Multi-level Inheritance: In this concept, one class can inherit from a derived class. So the derived class becomes the base class for the class that inherits.
  • Hierarchical Inheritance: In this inheritance, one class is shared by numerous sub-classes.
  • Hybrid Inheritance: It is a combination of single and multiple inheritances, and it can be achieved in Java.

How Can Inheritance be best explained in Java?

When there exists ‘IS-A’ relationship between the two classes, inheritance can be used in Java.  We have already discussed that parent class is termed as super-class, and the class that inherits is known as a subclass. Java has a reserved keyword ‘extend’ that is used by the subclass to inherit the features of a superclass. As inheritance leads to the reusability of the code, so it is critical to have. Here is the syntax of Java:

The syntax of Java Inheritance:

class subClass extends superClass 

// data and functions here

} 

Here is an example to use Java inheritance:   

class Cube { void Cube_Details() {  System.out.println(“Cube Dimensions.”); }}class Cuboid extends Cube { void Cuboid_Details() {  System.out.println(“Cuboid Dimensions.”); }}public class Figures {public static void main(String args[]) {Cube c = new Cube();  c.Cuboid_Details();  c.Cube_Details(); }}

So these were basics of inheritance in Java. But when it comes to facing interview questions on inheritance, it is better to be prepared with a list of questions and their answers.

Interview Question-1: What is inheritance in Java?

Answer-1: It is an object-oriented feature that allows a class to inherit data and methods from another class. A simple way to understand the concept is by way of an example. In real life, under normal circumstances, Father inherits from Grand-Father and Son inherits from Father.

Interview Question-2: Can you tell which kind of inheritances is supported in Java?

Answer-2: In Java single and multilevel inheritance is supported.

Interview Question-3: Which inheritance is not supported Java?

Answer-3: A class does not support multiple inheritances.

Interview Question-4: Which is it not supported Java?

Answer-4: Java came after C++, and the creators of core Java wanted to take from C++ only that was required and unambiguous. But they were not convinced about multiple inheritances because of the complexity and confusion it brings.

Interview Question-5: Why do Java programmers use inheritance?

Answer-5: The inheritance is used in Java to reuse the existing code mainly.

Interview Question-6: How can you use inheritance in Java?

Answer-6: Using inheritance in Java is simple, you can do so by extending classes and also by implementing interfaces. The two keywords that are used to apply are:

  • Extends
  • Implements

The class that is derived from a parent class is known as sub-class. In the same, the interface that is derived from another interface is known as sub-interface. ‘Implementation’ is the class that implements an interface.

Interview Question-7: Differentiate between inheritance and encapsulation.

Answer-7: By using inheritance, you can create a parent-child relationship between two classes. It allows usage of code written for the parent class, but it also forms the basis for Polymorphism. But when you talk about encapsulation, it is an OOP concept which you can use to hide the internal details of a class. Sphere class encapsulates the data and the methods to calculate volume, surface area.

In The End

If you want to succeed in Java interview, you have to know the basics of inheritance in java and also practice interview questions to taste success!

Leave a Reply