Inheritance Program in Java

Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help of an existing class. This newly created class is also known as the derived class, or child class, or subclass, whereas the existing class is known as the base class or parent class, or superclass. The child class acquires the characteristics of the parent class. Inheritance is widely used in the world of programming world as it manages the information in hierarchical order (parent to child, child to sub-child, and so on). The main advantages of using inheritance are:

1) Helps in achieving re-usability of code.

2) Facilitates method overriding in the child class.

Inheritance is mainly used where there is an IS-A relationship. For example,

  • Crow is a bird.
  • Python is a reptile.
  • An employee is a human being.
  • Apple is a fruit.
  • A Dentist is a doctor.

In the above examples, Crow, Python, Employee, Apple, and Dentist are the child classes, whereas Bird, Reptile, Human Being, Fruit, and Doctor are the parent classes. The Inheritance program in Java shows how the parent-child relationship is achieved via programming.

Syntax:

In Java, the keyword extends is used to achieve inheritance. In the above code snippet, the class XYZ is acquiring the features of the class ABC. Therefore, XYZ is the child class, and ABC is the base class.

Re-usability of code in Inheritance

Consider the following code.

FileName: MainClass.java

Output:

Explanation: In the code, the string field fruitName is declared only in the Fruit class. The two-child classes: Apple and Banana, are inheriting from the Fruit class. Therefore, the string field fruitName is present in the class Apple and Banana. In the driver method, we have created two objects: one each for the class Apple and Banana. Using the created objects, we are accessing the field fruitName for both classes. Thus, we see that whatever we declare in the parent class, the child class automatically gets access to it. We do not have to make the declaration explicitly. This is how the re-usability of code is achieved. Similar to fields, in methods also we can do re-usability of code.

Method Overriding in Inheritance

The method defined in the parent class can get overridden in the child class. The overriding method means changing the definition of the method in the child class. Let’s understand with the help of a program.

FileName: MainClass.java

Output:

Explanation: The method seeds() is being overridden in the Banana class. Note that, in overriding a method, the definition of the method changes, not it’s signature. Upon invoking the method seeds() on the object banana,  the overridden version of the method seeds() is called (observe the output). To stop the overriding, comment/ delete the method seeds() in the child class (do it yourself, and then observe what changes occur in the output.). Before overriding the method, it is a good practice to add “@Override”. This enhances the readability of the code. However, omitting “@Override” does not have any impact on the output.

Note: While overriding a method in Java, we cannot specify a weaker access specifier to an overridden method in the child class. Let’s see what happens if we add the keyword public in the method seeds() of the class Fruit.

FileName: MainClass.java

Output: Compilation Error

  attempting to assign weaker access privileges; was public

1 error

Explanation: The method seeds() has the public access specifier in the parent class, whereas, in the child class, seeds() has the default access specifier, which is weaker than the public access specifier. Thus, we are reducing the visibility of the method seeds(). Hence, we get the compilation error on the execution of the above program.

Types of Inheritance in Java

There are five types of inheritance in Java.

1) Single Inheritance

Inheritance Program in Java

In the above diagram, class Y is acquiring the properties of class X. Therefore, class X is the parent class, and class Y is the child class.  The following Java program also demonstrates the same.

Output:

2) Multi-level Inheritance

Inheritance Program in Java

Multi-level inheritance is the extension of the single inheritance. In the above diagram, class Y is acquiring the properties of class X, and class Z is inheriting properties from class Y. Thus, class Y is the child class as it is inheriting properties from class X. Class Y is also the parent class as class Z is acquiring the properties of class Y. Consider the following example.

Output:

3) Hierarchical Inheritance

Inheritance Program in Java

In hierarchical inheritance, one parent class has more than one child. In the given diagram, class Y and class Z are inheriting properties from class X. The following example illustrates the same.

Output:

4) Multiple Inheritance

Inheritance Program in Java

The opposite of multiple inheritance is hierarchical inheritance. In hierarchical inheritance, one child class has more than one parent. Note that multiple inheritance is not supported in Java. To understand the reason behind it, consider the following example.

Filename: MultipleInheritanceExample.Java

Output: Compilation Error

Explanation: Class Z is the child class of class X and class Y. Also, the parent classes X and Y contain the method foo() of the same signature. Therefore, the child class Z inherits both the methods foo() mentioned in the classes X and Y. This leads to a dilemma when the method z.foo() is executed because it is not clear whether z.foo() invoking the method foo() of class X or of the class Y. Therefore, to completely eliminate this scenario, Java never supports multiple inheritance.

5) Hybrid Inheritance

Inheritance Program in Java

The Hybrid inheritance is the mixture of multiple and hierarchical inheritance. The upper part, where class Y and class Z are the children of class X, represents hierarchical inheritance, whereas the lower part, where the class XYZ is acquiring the properties of class Y and class Z, represents multi-level inheritance. Since multi-level inheritance is part of the hybrid inheritance, therefore Java does not support hybrid inheritance.

Pin It on Pinterest

Share This