Polymorphism in Java

Introduction

 
In this article, we describe Polymorphism, the most popular Object Oriented Programming (OOP) concept, in Java. This article also describes how it can be achieved in the Java programming language, where it's possible and the benefits of polymorphism and its definition.
 

Polymorphism in Java

 
Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word that means "many-shaped". Generally, polymorphism refers to the ability to appear in many forms.

Now Polymorphism in Java programming terms of polymorphism is the ability of a reference variable to change behavior according to what object instance it is holding. And it allows multiple objects of different subclasses to be treated as objects of a single super class, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to.
 
0.jpg 
 
This image shows that we provide only one functionality, that is speech, which can vary depedning on the animal. In this image there are three animals that are represented, all three having the ability to speak but their sound is not same; it changes according to the variety of animal. In terms of programming we want to implement the same thing, that's why we use polymorphism.
 

Benefit Of Polymorphism

 
1-Simplicity
  • If you need to write code that deals with a family of types, the code can ignore type-specific details and just interact
    with the base type of the family.
  • Even though the code thinks it is using an object of the base class, the object's class could actually be the base class or any one of its subclasses.
  • This makes your code easier for you to write and easier for others to understand.
2-Extensibility
  • Other subclasses could be added later to the family of types, and objects of those new subclasses would also work with the existing code.

How To Achieve Polymorphism

 

Method Overloading

 
We can achieve run time polymorphism by overloading the method. In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism.
 
Example
  1. class OverloadingExample  
  2. {  
  3.     void Method() {  
  4.         System.out.println("No parameters");  
  5.     }  
  6.     // Overload test for one integer parameter.  
  7.     void Method(int a) {  
  8.         System.out.println("a: " + a);  
  9.     }  
  10.     // Overload test for two integer parameters.  
  11.     void Method(int a, int b) {  
  12.         System.out.println("a and b: " + a + " " + b);  
  13.     }  
  14.     // overload test for a double parameter  
  15.     double Method(double a) {  
  16.         System.out.println("double a: " + a);  
  17.         return a * a;  
  18.     }  
  19. }  
  20. public class OverloadDemo {  
  21.     public static void main(String args[]) {  
  22.         OverloadingExample ob = new OverloadingExample();  
  23.         double result;  
  24.         // call all versions of test()  
  25.         ob.Method();  
  26.         ob.Method(10);  
  27.         ob.Method(1020);  
  28.         result = ob.Method(303.23);  
  29.         System.out.println("Result of ob.Method(123.2): " + result);  
  30.     }  
  31. }  
Output
 
1.jpg 
 

Method Overriding

 
The method overriding is the second way of achieving polymorphism because when you call a method by the child class object then the method is executed in the child class and when the same method name is execute by the super class object. Overriding: If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: the ability to define a behavior that's specific to the sub class type. Which means a subclass can implement a parent class method based on its requirement.
 
Example
  1. class SuperClass  
  2. {  
  3.     void display()  
  4.     {  
  5.         System.out.println("Hello friends i am from super class");  
  6.     }  
  7. }  
  8. public class OverrideDemo  
  9. {  
  10.     // here we override the display method     
  11.     void display()  
  12.     {  
  13.         System.out.println("Hello from child class");  
  14.     }  
  15.     public static void main(String arg[])  
  16.     {  
  17.         SuperClass s = new SuperClass();  
  18.         OverrideDemo d = new OverrideDemo();  
  19.         // first we call display method by super class object  
  20.         s.display();  
  21.         // now we call the same method by the chiuld class object the out put is differ  
  22.         d.display();  
  23.     }  
  24. }  
Output
 
2.jpg


Similar Articles