Reader Level:
ARTICLE

Polymorphism in Java

Posted by Abhishek Dubey Articles | Java May 23, 2012
In this article we describe Polymorphism, the most popular Object Oriented Programming (OOP) concept, in Java
  • 0
  • 0
  • 6673

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 :

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 it

1-ByMethod 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");

}

// Overload test for one integer parameter.

void Method(int a) {

System.out.println("a: " + a);

}

// Overload test for two integer parameters.

void Method(int a, int b) {

System.out.println("a and b: " + a + " " + b);

}

// overload test for a double parameter

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;

// call all versions of test()

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

 1.jpg


2-By 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

{

// here we override the display method   

void display()

{

System.out.println("Hello from child class");

}

public static void main(String arg[])

{

SuperClass s= new SuperClass();

OverrideDemo d= new OverrideDemo();

// first we call display method by super class object

s.display();

// now we call the same method by the chiuld class object the out put is differ

d.display();

}

}

Output

 2.jpg

COMMENT USING

Trending up