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.
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
- class OverloadingExample
- {
- void Method() {
- System.out.println("No parameters");
- }
-
- void Method(int a) {
- System.out.println("a: " + a);
- }
-
- void Method(int a, int b) {
- System.out.println("a and b: " + a + " " + b);
- }
-
- double Method(double a) {
- System.out.println("double a: " + a);
- return a * a;
- }
- }
- public class OverloadDemo {
- public static void main(String args[]) {
- OverloadingExample ob = new OverloadingExample();
- double result;
-
- ob.Method();
- ob.Method(10);
- ob.Method(10, 20);
- result = ob.Method(303.23);
- System.out.println("Result of ob.Method(123.2): " + result);
- }
- }
Output
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
- class SuperClass
- {
- void display()
- {
- System.out.println("Hello friends i am from super class");
- }
- }
- public class OverrideDemo
- {
-
- void display()
- {
- System.out.println("Hello from child class");
- }
- public static void main(String arg[])
- {
- SuperClass s = new SuperClass();
- OverrideDemo d = new OverrideDemo();
-
- s.display();
-
- d.display();
- }
- }
Output