In this post, I am going to discuss about Polymorphism one of the pillar of OOP in Java. First We will try to understand with real life example. In real life, A women at the same time can have different characteristic. Like a women at the same time is a mother, a wife, an employee. So the same women posses different behavior in different situations. We can say the nature of the women is polymorphic. Just like this in Java language, Java objects possess the same functionality where an object changes its behavior as per situation and this is known as Polymorphism. The word polymorphism comes from the Greek words which means many-forms/many-shapes. In this article, let’s discuss about Polymorphism one of the key concept of Object Oriented Programming language.
Below are the topics to be covered in this article:
What is Polymorphism?
Types of Polymorphism in Java
- Compile time Polymorphism
- Runtime Polymorphism
What is Polymorphism?
Polymorphism is the ability of object to take multiple forms. In other words, We can say Polymorphism is a concept by which we can perform a single action in different ways. Lets understand the above statement/definition with the help of real world example. A boy when he is in the classroom behaves like student but when he is in the market behaves like customer & when he is in home behaves like son or brother. Here boy behavior is different in different situation.
Types of Polymorphism
There are two types of polymorphism:
- Compile time Polymorphism / Static binding / Early binding
- Runtime Polymorphism / Dynamic binding / Late binding
Compile time Polymorphism
In Compile-time polymorphism, the method call is resolved at the compile time by the compiler. That's why it is known as Compile-time polymorphism. This type of binding is also known as static binding or early binding. We can achieve compile-time polymorphism by the help of method-overloading.
What is Method-Overloading?
Method Overloading is a feature that allows a class to have multiple methods with same name but different arguments in terms of sequence, type or number. Here We have to pay attention to one point that In java, method overloading is not possible by changing the return type of the method. Otherwise It will cause compile-time error i.e. Duplicate method. Lets understand method-overloading with the help of real life example. Whenever we do shopping on any e-commerce sites like Flipkart, Amazon then at the time of doing payment we have several options like COD, Net Banking, UPI etc. Here we have one method for doing the payment but it takes different arguments like Net Banking, UPI etc. and behaves accordingly.
This is how Static/Compile-time Polymorphism works. Now, let’s understand what is Runtime Polymorphism in Java.
In Runtime polymorphism, the method call is resolved by the JVM at the run time based on the object created. That's why it is know Run-time polymorphism. This type of binding is also known as dynamic binding or late binding. It is also known as Dynamic Method Dispatch. We can achieve run-time polymorphism by the help of method-overriding.
What is Method-Overriding?
Method-Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. Basically if the parent class has any method with some implementation then the method will be by default available to the child through inheritance if the child is not satisfied with parent class method implementation then child is allow to redefine that parent class method in child class in its own way. This behavior is known as method overriding. If we want to relate method overriding with real life example then Suppose There is a method getInterestRate() which returns the interest rate of a bank. RBI is the superclass and it returns 5.0 for getInterestRate(). There are various banks like SBI, AXIS, HDFC, BOI etc. which extend RBI class and if any bank is not happy with the RBI’s getInterestRate() implementation then the bank can override the getInterestRate() method to provide different implementation.
Rate of Interest: 6.5
In the above example, we have two classes RBI and SBI. RBI is a parent class and SBI is a child class. The child class is overriding the method getInterestRate() of the parent class. Here, I have assigned child class object to the parent class reference to determine which method would be called at run-time. It is the type of object that determines which version of the method would be called (not the type of reference). When you invoke the overriding method, then the object determines which method is to be executed. Thus, this decision is made at run time.
I have listed down few more overriding examples.
1. RBI rbi=new SBI();
System.out.println("Rate of Interest: "rbi.getInterestRate());// This would call the getInterestRate() of parent class SBI2. RBI rbi=new SBI();
System.out.println("Rate of Interest: "rbi.getInterestRate());
// The method of the child class is to be executed because the method that needs to be executed is determined by the type of object. Since the object belongs to the child class. This would call the getInterestRate() of child class SBI.3. SBI sbi=new SBI();
System.out.println("Rate of Interest: "sbi.getInterestRate()); // This would call the getInterestRate() of child class SBI
With this, we come to an end this article on Polymorphism in Java.