What is the Difference Between Inheritance and Polymorphism in Java

The main difference between Inheritance and Polymorphism in Java is that Inheritance allows a class to use the properties and methods of an already existing class while polymorphism allows an object to behave in multiple ways.

Object-Oriented Programming (OOP) is a common programming paradigm in software development. It helps to model real-world scenarios using objects. Java is a programming language that supports OOP. A class is a blueprint, and it helps to create objects. A class has attributes and behaviors. The attributes are also called properties while behaviors are also called methods. Two major pillars of OOP are Inheritance and Polymorphism.

Key Areas Covered

1. What is Inheritance in Java
     – Definition, Functionality
2. What is Polymorphism in Java
     – Definition, Functionality
3. What is the Difference Between Inheritance and Polymorphism in Java
     – Comparison of Key Differences

Key Terms

Class, Inheritance, Overloading, Overriding, Polymorphism, Java

Difference Between Inheritance and Polymorphism in Java - Comparison Summary

What is Inheritance in Java

Inheritance in Java is the mechanism that allows a class to use properties and behaviors of an already existing class. The already existing class is the parent class or superclass. The new class that inherits the properties and methods is called the child class or subclass. The main advantage of Inheritance is that it provides code reusability. An example program is as follows.

Difference Between Inheritance and Polymorphism in Java_Figure 1

Figure 1: Class A

Difference Between Inheritance and Polymorphism in Java_Figure 2

Figure 2: Class B

Difference Between Inheritance and Polymorphism in Java_Figure 3

Figure 3: Class Test

Class A has the method sum, and it adds two values. Class B extends A.  It has a method called sub. It subtracts two values. Test class has the main method. The ‘obj’ is an object of type B. As class B inherits class A, the object can use the properties and methods of class A. Therefore, the object can call both sum and sub-methods.

What is Polymorphism in Java

Polymorphism in Java refers to an object showing different behaviors at different stages of its life cycle. There are mainly two types in polymorphism as overloading and overriding.

Overloading

Overloading allows the methods in the same class or subclasses with the same name but with different parameters. It is also called ‘static binding’ and ‘compile-time polymorphism’. An example program is as follows.

Difference Between Inheritance and Polymorphism in Java

Figure 4: Java program with overriding

Overloading class has two methods with the same name as sum. The sum method in line 5 does not take any parameters. It adds the two values 10 and 20 and returns the result, which is 30. Moreover, the sum method in line 11 gets to two parameters. It adds those two values and returns the result 50. The obj is an object in the main method. The obj.sum() calls the sum method in line 5 and obj.sum(20,30) calls the sum method in line 11. The same object calls the associated method accordingly. Therefore, the same object has different behaviors depending on the situation.

Overriding

Overriding allows providing an implementation to a method that already exists in its superclass. It is also called ‘late binding’, ‘dynamic binding’ and ‘runtime polymorphism’. An example program is as follows.

Difference Between Inheritance and Polymorphism in Java_Figure 5

Figure 5: Vehicle class

Difference Between Inheritance and Polymorphism in Java_Figure 6

Figure 6: Car class

Main Difference - Inheritance vs Polymorphism in Java

Figure 7: Test class

Vehicle class has a method called display. Car class extends Vehicle class, and it also has a method called display with its own implementation. Vehicle is the superclass while Car is the subclass. Test class has the main method.  The ‘c’ is an object of type Car. When calling the display method, we can see the implementation of the display method in the Car class. Even though the superclass has the display method, it is overridden by the display method in the subclass.

Difference Between Inheritance and Polymorphism in Java

Definition

Inheritance is the mechanism of allowing a new class to use properties and methods of a superclass while polymorphism is the ability of an object to behave in multiple ways. Thus, this is the main difference between Inheritance and Polymorphism in Java.

Implementation

Moreover, implementation of inheritance occurs in class level whereas implementation of polymorphism occurs in method level.

Usage

Furthermore, while inheritance provides code reusability, polymorphism allows calling methods accordingly at compile time and runtime. Hence, this is another difference between Inheritance and Polymorphism in Java.

Conclusion

Two major pillars of OOP are Inheritance and Polymorphism. The main difference between Inheritance and Polymorphism in Java is that inheritance allows a class to use the properties and methods of an already existing class while polymorphism allows an object to behave in multiple ways.

Reference:

1. “Method Overloading in Java – Javatpoint.” Www.javatpoint.com, Available here.
2. “Method Overriding in Java – Javatpoint.” Www.javatpoint.com, Available here.
3. “Inheritance in Java – Javatpoint.” Www.javatpoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply