In the previous tutorial, we learned one of the three OOP concepts. i.e Java – Encapsulation.

In this tutorial, we will learn the remaining two OOP concept Inheritance in java and Polymorphism in java.

Before we go deep into what they are, first get an overview of it.

  • What is Inheritance? - When a "class" acquires the property of another class it is known as Inheritance. For example, a child inherits the traits of parents.
  • What is Polymorphism?- While Polymorphism means one name but many forms. For example, you have a smartphone for communication. The communication mode you choose could be anything. It can be a call, a text msg, a picture msg, mail, etc. So the goal is common that is communication, but their approach is different. This is called Polymorphism.

Consider the same banking application from the previous banking example.

Please be patient. The Video will load in some time. If you still face issue viewing video click here

We are supposed to open two different account types, one for saving and another for checking (also known as current).

Java Inheritance & Polymorphism

Let's compare and study how we can approach coding from a structured and object oriented programing perspective.

  • Structural approach: In structured programing, we will create two functions –
  1. One to withdraw
  2. And the other for deposit action.

Since the working of these functions remains same across the accounts.

Java Inheritance & Polymorphism

  • OOP's approach: While using the OOPs programming approach. We would create two classes.
    • Each having implementation of the deposit and withdraw functions.
    • This will redundant extra work.

    Java Inheritance & Polymorphism

Implementing Privileged Bank Account

Now there is a change in the requirement specification for something that is so common in the software industry. You are supposed to add functionality privileged Banking Account with Overdraft Facility. For a background, overdraft is that you can withdraw an amount more than available the balance in your account.

Java Inheritance & Polymorphism

  • Structural approach:

    Using functional approach, I have to modify my withdraw function, which is already tested and baselined. And add a method like below will take care of new requirements.

Java Inheritance & Polymorphism

  • OOP's approach:

    Using OOP's approach, you just need to write a new class with unique implementation of withdraw function. We never touched the tested piece of code.

Java Inheritance & Polymorphism

What if the requirement changes further? Like to add credit card account with its own unique requirement of deposits.

Java Inheritance & Polymorphism

  • Structural approach: Using structural approach you have to change tested piece of deposit code again.

Java Inheritance & Polymorphism

  • OOP's approach: But using object-oriented approach, you will just create a new class with its unique implementation of deposit method ( highlighted red in the image below).

So even though the structural programming seems like an easy approach initially, OOP's wins in a long term.

Java Inheritance & Polymorphism

Challenge in OOP's

But one may argue that across all classes, you have a repeated pieces of code.

To overcome this, you create a parent class, say "account" and implement the same function of deposit and withdraw. And make child classes inherited "account" class. So that they will have access to withdraw and deposit functions in account class.

The functions are not required to be implemented individually. This is Inheritance in java .

Java Inheritance & Polymorphism

Java Inheritance & Polymorphism

Java Polymorphism

But wait there is a problem!

Withdraw method for privileged and deposit for a credit card is different.

To overcome this, you can override the method implementation in your base(child) class.

Java Inheritance & Polymorphism

  • Such that when the "withdrawn" method for saving account is called method from parent account class is executed.

Java Inheritance & Polymorphism

  • But when the "Withdraw" method for the privileged account (overdraft facility) is called withdraw method defined in the priveldged class is executed. This is Polymorphism.

Java Inheritance & Polymorphism


  • Encapsulation, Inheritance, and Polymorphism are all based on OOP's concept
  • When a class acquires the property of another class, it is known as inheritance.
  • Polymorphic means one name but many forms
  • Use override method to implement the function of the derived class (child class).