Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

JAVA – PROGRAMMING

POLYMORPHISM

▪ Polymorphism is the ability of an object to take on different forms. In Java, polymorphism


refers to the ability of a class to provide different implementations of a method, depending
on the type of object that is passed to the method.
▪ Polymorphism in Java is the task that performs a single action in different ways.
▪ Polymorphism occurs when there is inheritance, i.e., many classes are related to each
other.
▪ Inheritance is a powerful feature in Java. Java Inheritance lets one class acquire the
properties and attributes of another class. Polymorphism in Java allows us to use these
inherited properties to perform different tasks. Thus, allowing us to achieve the same action
in many ways.

▪ The derivation of the word Polymorphism is from two different Greek words- poly and
morphs. “Poly” means numerous, and “Morphs” means forms. So, polymorphism means
innumerable forms. Polymorphism, therefore, is one of the most significant features of
Object-Oriented Programming.

Types of Polymorphism
You can perform Polymorphism in Java via two different methods:

1. Method overloading is the process that can create multiple methods of the same
name in the same class, and all the methods work in different ways. Method
overloading occurs when there is more than one method of the same name in the
class.
2. Method overriding is the process when the subclass or a child class has the same
method as declared in the parent class.

1|P age – Java Progr amming


Example of Method Overloading in Java

Public class Shapes {

public void area() {

System.out.println("Find area ");

} //end of area method without paramerater

public void area(int r) {

System.out.println("Circle area = "+3.14*r*r);

} //end of area method with one parameter

public void area(double b, double h) {

System.out.println("Triangle area="+0.5*b*h);

} // end of area method with parameters

public void area(int l, int b) {

System.out.println("Rectangle area="+l*b);

} // end of area method having two parameters but the data type is different from the other method

} //end of shape class

public class Main {

public static void main(String[] args) {

Shapes myShape = new Shapes(); // Create a Shapes object

myShape.area();

myShape.area(5);

myShape.area(6.0,1.2);

myShape.area(6,2);

Output: Find area


Circle area = 78.5
Triangle area=3.60
Rectangle area=12

2|P age – Java Progr amming


public class Shape{
public void render(){
System.out.println("Shape");
}
}
..........................................................

public class Square extends Shape{


@Override
public void render(){
System.out.println("Square");
}
}
..........................................................

public class Circle extends Shape{


@Override
public void render(){
System.out.println("Circle");
}
}
..........................................................
public class Rectangle extends Shape{
@Override
public void render(){
System.out.println("Rectangle");
}
}
..........................................................
public class Triangle extends Shape{
@Override
public void render(){
System.out.println("Triangle");
}
}
public class Main{

public static void main(String[] args){

Shape[] shapes = {new Circle(), new Triangle(),new Rectangle(), new Square()};

//changing the implementation of the parent class

for(Shape shape:shapes){

shape.render();

}//end of the main method


3|P age – Java Progr amming
}
Output:
Circle
Triangle
Rectangle
Square

Two types of polymorphism

1. Static/Compile-Time Polymorphism
2. Dynamic/Runtime Polymorphism

Compile Time Polymorphism In Java is also known as Static Polymorphism. Furthermore,


the call to the method is resolved at compile-time. Compile-Time polymorphism is
achieved through Method Overloading. This type of polymorphism can also be achieved
through Operator Overloading. However, Java does not support Operator Overloading.

Method Overloading is when a class has multiple methods with the same name, but the
number, types, and order of parameters and the return type of the methods are different.
Java allows the user freedom to use the same name for various functions as long as it can
distinguish between them by the type and number of parameters. Check out some of the
important questions on run time polymorphism in java interview questions.

Runtime Polymorphism. It is also known as Dynamic Method Dispatch. It is a process in


which a function call to the overridden method is resolved at Runtime. This type of
polymorphism is achieved by Method Overriding. Method overriding, on the other hand,
occurs when a derived class has a definition for one of the member functions of the base
class. That base function is said to be overridden.

Advantages of Polymorphism in Java:

1. Increases code reusability by allowing objects of different classes to be treated as


objects of a common class.
2. Improves readability and maintainability of code by reducing the amount of code
that needs to be written and maintained.
3. Supports dynamic binding, enabling the correct method to be called at runtime,
based on the actual class of the object.
4. Enables objects to be treated as a single type, making it easier to write generic
code that can handle objects of different types.
4|P age – Java Progr amming
Disadvantages of Polymorphism in Java:

1. Can make it more difficult to understand the behavior of an object, especially if the
code is complex.
2. May lead to performance issues, as polymorphic behavior may require additional
computations at runtime.

5|P age – Java Progr amming


References:
▪ https://www.mygreatlearning.com/blog/polymorphism-in-java/
▪ https://www.geeksforgeeks.org/polymorphism-in-java/

Prepared by: Mrs. J. C. De Vera

6|P age – Java Progr amming

You might also like