This post talks about one of the OOPS concept polymorphism and the usage of polymorphism in Java.
What is polymorphism
Polymorphism is one of the four fundamental principles of Object Oriented Programming along with inheritance, abstraction and encapsulation.
Polymorphism is a Greek word where poly means “many” and morph means “change from one form to another”. In object oriented terms it relates to the same object reference taking many forms.
The concept of polymorphism in Java is designed as an interface having a method and the derived classes implementing the interface as per their requirement. Then using the reference of that single interface any of those implemented class methods can be invoked. So one interface reference can take many forms here based on which class it is referring to.
Polymorphism in Java
There are two types of Polymorphism in Java-- Compile time polymorphism- Also known as static polymorphism. Static polymorphism in Java is achieved through Method overloading as it is known at compile time itself which of the overloaded method will be called.
- Runtime polymorphism- Also known as dynamic polymorphism. Dynamic polymorphism in Java is achieved through Method overriding. In method overriding it is resolved at run time which of the overridden method would be called.
Example of Run time polymorphism in Java
In the example there is an interface Payment with a method doPayment(double amount). This interface is implemented by two classes.
public interface Payment { void doPayment(double amount); }Implementing class -1
public class CashPayment implements Payment { @Override public void doPayment(double amount) { System.out.println("Cash payment of amount- " + amount); } }Implementing class -2
public class CCPayment implements Payment { @Override public void doPayment(double amount) { System.out.println("Swipe credit card for amount- " + amount); } }
public class MainClass { public static void main(String[] args) { // Reference of CashPayment instance Payment payment = new CashPayment(); payment.doPayment(106.78); // Now holding reference of CCPayment instance payment = new CCPayment(); payment.doPayment(77.67); } }Output
Cash payment of amount- 106.78 Swipe credit card for amount- 77.67
As you can see at run time reference of Payment can refer to any of these implementations and based on the current reference that class’ method is called.
Example of Compile time polymorphism in Java
public class MainClass { public static void main(String[] args) { MainClass obj = new MainClass(); System.out.println("Addition Result- " + obj.add(7, 8)); System.out.println("Addition Result- " + obj.add(123.56, 34.78)); } public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }
Here in the class there are two methods with the same name (add) but the types of parameters are different so these are overloaded methods. Based on the types of arguments passed in the method call appropriate add method is called. This binding is done at the compile time itself.
OutputAddition Result- 15 Addition Result- 158.34
That's all for the topic Polymorphism in Java – OOPS Concepts. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Abstract Class in Java
- Constructor Overloading in Java
- Java Stream API With Examples
- Fail-fast And Fail-safe Iterators in Java
- Can we Directly Call run() Method Instead of Calling start() Method in Java
- Fibonacci Series Java Program
- Constructor Dependency Injection in Spring
- How to Delete File and Directory in Python
No comments:
Post a Comment