Polymorphism in Java OOPs with Example

Polymorphism in Java OOPs with Example

What is Polymorphism?

Polymorphism is a OOPs concept where one name can have 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 message, a picture message, mail, etc. So, the goal is common that is communication, but their approach is different. This is called Polymorphism.

In this tutorial, you will learn-

Click here if the video is not accessible

Java Polymorphism in OOP’s with Example

We have one parent class, ‘Account’ with function of deposit and withdraw. Account has 2 child classes

The operation of deposit and withdraw is same for Saving and Checking accounts. So the inherited methods from Account class will work.

Java Inheritance & Polymorphism

Change in Software Requirement

There is a change in the requirement specification, 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 a facility where you can withdraw an amount more than available the balance in your account.

So, withdraw method for privileged needs to implemented afresh. But you do not change the tested piece of code in Savings and Checking account. This is advantage of OOPS

Java Inheritance & Polymorphism

Step 1) Such that when the “withdrawn” method for saving account is called a method from parent account class is executed.

Java Inheritance & Polymorphism


Step 2) But when the “Withdraw” method for the privileged account (overdraft facility) is called withdraw method defined in the privileged class is executed. This is Polymorphism.

Java Inheritance & Polymorphism

Method Overriding

Method Overriding is redefining a super class method in a sub class.

Rules for Method Overriding

  • The method signature i.e. method name, parameter list and return type have to match exactly.
  • The overridden method can widen the accessibility but not narrow it, i.e. if it is private in the base class, the child class can make it public but not vice versa.

Concept of Inheritance Java and Java Polymorphism


obj.treatPatient() will execute treatPatient() method of the sub-class – Surgeon

If a base class reference is used to call a method, the method to be invoked is decided by the JVM, depending on the object the reference is pointing to

For example, even though obj is a reference to Doctor, it calls the method of Surgeon, as it points to a Surgeon object

This is decided during run-time and hence termed dynamic or run-time polymorphism

Super Keyword

What if the treatPatient method in the Surgeon class wants to execute the functionality defined in Doctor class and then perform its own specific functionality?

In this case, keyword supercan be used to access methods of the parent class from the child class.

The treatPatient method in the Surgeon class could be written as:

Step 2) Save, Compile & Run the code. Observe the output.

Step 3) Uncomments lines # 6-9. Save, Compile & Run the code. Observe the output.

Step 4) Uncomment line # 10 . Save & Compile the code.

Step 5) Error = ? This is because sub-class cannot access private members of the super class.

Difference between Static & Dynamic Polymorphism

Glad to have you at Our Website
Welcome to WPBot