In this Java tutorial, you will learn-

What is Polymorphism in Java? Java Polymorphism in OOPs with Example Method Overriding in Java Difference between Overloading and Overriding What is Dynamic Polymorphism? Difference between Static & Dynamic Polymorphism Super Keyword in Java

Java Polymorphism in OOPs 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.

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

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

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 in OOPs.

Method Overriding in Java

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.

Example

Difference between Overloading and Overriding

What is Dynamic Polymorphism?

Dynamic Polymorphism in OOPs is the mechanism by which multiple methods can be defined with same name and signature in the superclass and subclass. The call to an overridden method are resolved at run time.

Dynamic Polymorphism Example:

A reference variable of the super class can refer to a sub class object Consider the statement Here the reference variable “obj” is of the parent class, but the object it is pointing to is of the child class (as shown in the below diagram example of 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 in Java

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: The keyword super can be used to access any data member or methods of the super class in the sub class. Next, we will learn about Super keyword, Inheritance and Polymorphism in Java with example programs. Example:-To learn Inheritance, Polymorphism & super keyword Step 1) Copy the following code into an Editor 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.

Summary

Polymorphism in Object Oriented Programming occurs when there are one or more classes or objects related to each other by inheritance. It is the ability of an object to take many forms. Method Overriding is redefining a super class method in a sub class. Dynamic Polymorphism in Java is the mechanism by which multiple methods can be defined with same name and signature in the superclass and subclass. Static Polymorphism in Java is a type of polymorphism that collects the information for calling a method at compilation time, whereas Dynamic Polymorphism is a type of polymorphism that collects the information for calling a method at runtime. Super keyword can be used to access methods of the parent class from the child class.