0% found this document useful (0 votes)
74 views

Polymorphism-in-Java 17

Polymorphism in Java allows the same method to perform different operations based on the object it is acting upon. There are two main types: compile-time polymorphism through method overloading, and run-time polymorphism through method overriding. In method overloading, the compiler determines which method to call based on arguments, while in overriding the actual implementation is determined at runtime based on the object's type. Polymorphism provides benefits like flexibility, extensibility, and abstraction.

Uploaded by

mohit chouhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Polymorphism-in-Java 17

Polymorphism in Java allows the same method to perform different operations based on the object it is acting upon. There are two main types: compile-time polymorphism through method overloading, and run-time polymorphism through method overriding. In method overloading, the compiler determines which method to call based on arguments, while in overriding the actual implementation is determined at runtime based on the object's type. Polymorphism provides benefits like flexibility, extensibility, and abstraction.

Uploaded by

mohit chouhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Programming

Topperworld.in

Polymorphism in Java

Polymorphism is an important concept of object-oriented programming.


It simply means more than one form.

That is, the same entity (method or operator or object) can perform
different operations in different scenarios.

Let us understand the definition of polymorphism by an example; a lady can

have different characteristics simultaneously. She can be a mother, a


daughter, or a wife, so the same lady possesses different behavior in
different situations.

Example: Java Polymorphism


class Polygon {

// method to render a shape


public void render() {
System.out.println("Rendering Polygon...");
}
}

class Square extends Polygon {

// renders Square
public void render() {
System.out.println("Rendering Square...");
}
}

class Circle extends Polygon {

// renders circle
public void render() {
System.out.println("Rendering Circle...");
Java Programming

}
}

class Main {
public static void main(String[] args) {

// create an object of Square


Square s1 = new Square();
s1.render();

// create an object of Circle


Circle c1 = new Circle();
c1.render();
}
}

Output

Rendering Square...
Rendering Circle...

In the above example,

we have created a superclass: Polygon and two subclasses: Square and


Circle. Notice the use of the render() method.

The main purpose of the render() method is to render the shape. However,
the process of rendering a square is different than the process of rendering
a circle.

Hence, the render() method behaves differently in different classes.

Types of Polymorphism
 Compile-time polymorphism
 Run-time polymorphism
Java Programming

 Compile-time Polymorphism :
Compile-time polymorphism in Java is
also called static polymorphism or static method dispatch. It can be
achieved by method overloading. In this process, an overloaded method is
resolved at compile time rather than resolving at runtime.

Java Method Overloading

In a Java class, we can create methods with the same name if they differ
in parameters. For example,

void func() { ... }


void func(int a) { ... }
float func(double a) { ... }
float func(int a, float b) { ... }

This is known as method overloading in Java. Here, the same method will
perform different operations based on the parameter.

Example : Polymorphism using method overloading

class Pattern {

// method without parameter


public void display() {
for (int i = 0; i < 10; i++) {
System.out.print("*");
}
}

// method with single parameter


public void display(char symbol) {
for (int i = 0; i < 10; i++) {
System.out.print(symbol);
}
}
}

class Main {
public static void main(String[] args) {
Java Programming

Pattern d1 = new Pattern();

// call method without any argument


d1.display();
System.out.println("\n");

// call method with a single argument


d1.display('#');
}
}

Output:

**********

##########

In the above example, we have created a class named Pattern. The class

contains a method named display() that is overloaded.

// method with no arguments


display() {...}

// method with a single char type argument


display(char symbol) {...}

Here, the main function of display() is to print the pattern. However,


based on the arguments passed, the method is performing different
operations:
 prints a pattern of *, if no argument is passed or

 prints pattern of the parameter, if a single char type argument is


passed.
Java Programming

 Run-time Polymorphism :
Run-time polymorphism in Java is also
called Dynamic method dispatch. It can be achieved by method overriding.
In this process, Instead of resolving the overridden method at compile-
time, it is resolved at runtime.

Java Method Overriding

If the same method is present in both the superclass and the subclass.
Then, the method in the subclass overrides the same method in the
superclass. This is called method overriding.
In this case, the same method will perform one operation in the superclass
and another operation in the subclass. For example,

Example : Polymorphism using method overriding

class Language {
public void displayInfo() {
System.out.println("Common English Language");
}
}

class Java extends Language {


@Override
public void displayInfo() {
System.out.println("Java Programming Language");
}
}

class Main {
public static void main(String[] args) {

// create an object of Java class


Java j1 = new Java();
j1.displayInfo();

// create an object of Language class


Language l1 = new Language();
l1.displayInfo();
Java Programming

}
}

Output:

Java Programming Language


Common English Language

In the above example,


we have created a superclass named Language and a subclass

named Java.

Here, the method displayInfo() is present in both Language and Java.

The use of displayInfo() is to print the information. However, it is printing

different information in Language and Java.

Characteristics of Polymorphism :

 Overloading: Polymorphism in Java is achieved through method


overloading, which allows multiple methods to have the same name
but different parameters or argument types. The Java compiler
determines which method to call based on the arguments provided
at compile time.
 Overriding: Polymorphism in Java is also achieved through method
overriding, where a subclass provides a different implementation of
a method that is already defined in its parent class. The subclass
method must have the same name and signature as the parent class
method. When the method is called on an object of the subclass, the
subclass method is executed instead of the parent class method.
Java Programming

 Dynamic binding: Polymorphism in Java also involves dynamic


binding, where the actual implementation of a method is determined
at runtime instead of compile time. This allows for more flexible and
adaptable code as objects can be manipulated and used in different
ways.
 Flexibility: Polymorphism in Java allows objects of different classes
to be treated as if they were of the same class, making the code more
flexible and reusable.
 Extensibility: Polymorphism in Java allows for more modular and
extensible code as it enables objects to be manipulated and used in
different ways, making it easier to add new functionality or modify
existing code.
 Abstraction: Polymorphism in Java is closely related to abstraction,
as it allows objects to be abstracted into a common interface or
superclass, allowing for more generic code that can handle different
types of objects.

You might also like