Comparison between Inheritance and Polymorphism

Srushti Shingade
10 min readJun 7, 2022

--

Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviour of a parent object. It is an important part of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Terms used in Inheritance

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

The syntax of Java Inheritance

class Subclass-name extends Superclass-name

{

//methods and fields

}

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.

In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass.

Importance of Java inheritance

Implementation of inheritance in Java provides the following benefits:

  • Inheritance minimizes the complexity of a code by minimizing duplicate code. If the same code has to be used by another class, it can simply be inherited from that class to its sub-class. Hence, the code is better organized.
  • The efficiency of execution of a code increases as the code is organized in a simpler form.
  • The concept of polymorphism can be used along with inheritance.

Types of Java Inheritance

The different types of inheritance are observed in Java:

1. Single level inheritance

As the name suggests, this type of inheritance occurs for only a single class. Only one class is derived from the parent class. The flow diagram of a single inheritance is shown below:

Graphical illustration of single-level inheritance

Two classes Class A and Class B are shown in Figure , where Class B inherits the properties of Class A.

2. Multi-level Inheritance

The multi-level inheritance includes the involvement of at least two or more than two classes. One class inherits the features from a parent class and the newly created sub-class becomes the base class for another new class.

Flow diagram of multi-level inheritance

From the flow diagram in Figure, we can observe that Class B is a derived class from Class A, and Class C is further derived from Class B. Therefore the concept of grandparent class comes into existence in multi-level inheritance. However, the members of a grandparent’s class cannot be directly accessed in Java.

Therefore, Figure shows that Class C is inheriting the methods and properties of both Class A and Class B.

3. Hierarchical Inheritance

The type of inheritance where many subclasses inherit from one single class is known as Hierarchical Inheritance.

Graphical representation of a hierarchical inheritance

In Figure, we can observe that the three classes Class B, Class C, and Class D are inherited from the single Class A. All the child classes have the same parent class in hierarchical inheritance.

Other than these types of inheritance in Java, there are other types known as multiple inheritances and hybrid inheritance. Both types are not supported through classes and can be achieved only through the use of interfaces.

4. Multiple Inheritance

Multiple inheritances is a type of inheritance where a subclass can inherit features from more than one parent class.

Representation of multiple inheritances

Figure shows that Class C is derived from the two classes Class A and Class B. In other words it can be described that subclass C inherits properties from both Class A and B.

5. Hybrid Inheritance

Hybrid inheritance is a type of inheritance that combines single inheritance and multiple inheritances. As multiple inheritances is not supported by Java, hybrid inheritance can also be achieved through interfaces only.

Representation of a hybrid inheritance

With the different types of inheritance in Java, the ultimate aim is to create sub-classes having properties inherited from the superclasses. The created sub-classes have various properties which are:

  • Fields and methods inherited in a subclass can be directly used.
  • New fields and methods can also be declared in the subclass which is not present in the superclass.
  • A new instance method can be created in the subclass having the same signature as the method in the superclass. The process is referred to as overriding.
  • A new static method can be created in the subclass having the same signature as the method in the superclass. The process is referred to as hiding.

Polymorphism in Java

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

Types of polymorphism

If you overload a static method in Java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java.

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.

The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.

A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

Runtime Polymorphism in Java

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

Exception Handling in Java

In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.

Let’s first understand the upcasting before Runtime Polymorphism.

Upcasting

If the reference variable of Parent class refers to the object of Child class, it is known as upcasting. For example:

class A{}

class B extends A{}

A a=new B();//upcasting

For upcasting, we can use the reference variable of class type or an interface type. For Example:

interface I{}

class A{}

class B extends A implements I{}

Here, the relationship of B class would be:

B IS-A A

B IS-A I

B IS-A Object

Since Object is the root class of all classes in Java, so we can write B IS-A Object.

Virtual Methods

In this section, I will show you how the behavior of overridden methods in Java allows you to take advantage of polymorphism when designing your classes.

We already have discussed method overriding, where a child class can override a method in its parent. An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method.

Example

/* File name : Employee.java */
public class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public void mailCheck()
{
System.out.println("Mailing a check to " + this.name + " " + this.address);
}
public String toString()
{
return name + " " + address + " " + number;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber()
{
return number;
}
}

Key Difference Between Polymorphism vs Inheritance

Let us discuss some of the major key differences between Polymorphism vs Inheritance:

  • Inheritance in OOPs means that there is a subclass also known as a derived class that inherits the properties of the base class also called a superclass. So when the subclass inherits the superclass all the members of the base class become the members of that subclass also whereas in Polymorphism, ‘Poly’ means ‘many’ ‘morph’ means ‘forms’, so it defines the method of performing any task in various forms.
  • The main advantage of Inheritance in OOPs is that it allows the reusability of code. Reusability means that the programmer does not need to write the general code (which is the same as all the classes) instead only the specific code for each class needs to be written. In order to use the general code, specialized classes just need to inherit the main class (base class) whereas Polymorphism allows simplicity, easily manageable code by providing the advantage of a function having many forms and letting the function decide which form to invoke at the compile time and run time.
  • Inheritance allows the programmer to create hierarchies of the class by one class inheriting the other class giving its attributes and behavior whereas Polymorphism allows the user to create different methods for different requirements and ensure that the proper method would be executed on the basis of calling the object type.
  • Let’s understand with the help of an example, there is an Animal class having the basic properties of an Animal. There are Dog, Cat, Rabbit, etc classes that inherit the Animal class as all the Animal have some common properties. But the classes Dog, Cat, Rabbit can have their specialized method defining their individual properties, this is called Inheritance whereas, in Polymorphism, there is a method called as Area, calculating the area of geometric figures. So for the figures Rectangle, Square, Triangle there are different numbers of parameters like for square, there is one parameter required, for rectangle both the length and breadth are required and for triangle base and height are required. So the ‘Area’ method needs to be implemented differently for each geometric figure.
  • Polymorphism, it is of basically 2 types, i.e. run-time polymorphism which is known as method overriding (achieved through the inheritance, when one class inherits the base class) and second is compile-time polymorphism (which is achieved when the different methods are defined in the same class with different signatures) which is known as method overloading whereas with Inheritance one can go crazy and can create any levels depending on the requirements like Single, Multiple, Multilevel, Hierarchical and Hybrid. Though Java, which is an OOP language, does not support multiple inheritances.
  • As we have understood, Inheritance is applied to the classes when one class inherits all the properties, attributes, and methods of an already defined class but Polymorphism is applied to the functions/ methods of the classes as it involves defining the methods with the same name and different signatures. Though run-time Polymorphism is applied by implementing the Inheritance in the classes only.
  • Talking about the lines of code in the program, Inheritance reduces the lines of code as the child class reuses the code of the parent class by inheriting it whereas, in Polymorphism, the overall length of the program gets increased as a single method is implemented in different ways.

Difference between Inheritance and Polymorphism:

Conclusion

The above description clearly explains what Polymorphism and Inheritance are and the basic difference between the two. Both Polymorphism and Inheritance are the fundamental concepts of Object Oriented Programming. So in order to write the real OOPs program, it is important for the programmer to understand each of them clearly as they both are used for 2 different purposes as Inheritance allows the code reusability whereas Polymorphism allows defining of a single function in many ways.

SY_IT_A : Group2_Batch2

Group members:

Rajan Ner- 36,

Hitashri Patil- 42,

Rohant Narang- 51,

Nupur Shinde- 56,

Srushti Shingade- 57.

--

--