0% found this document useful (0 votes)
30 views12 pages

Polymorphism

The document discusses polymorphism in Java, defining it as the ability of a message to be displayed in multiple forms, and categorizing it into compile-time and runtime polymorphism. It explains method overloading and overriding, providing examples and detailing their advantages and disadvantages. The document emphasizes the importance of polymorphism in enhancing code reusability, readability, and maintainability while also noting potential complexities and performance issues.

Uploaded by

roopam12cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views12 pages

Polymorphism

The document discusses polymorphism in Java, defining it as the ability of a message to be displayed in multiple forms, and categorizing it into compile-time and runtime polymorphism. It explains method overloading and overriding, providing examples and detailing their advantages and disadvantages. The document emphasizes the importance of polymorphism in enhancing code reusability, readability, and maintainability while also noting potential complexities and performance issues.

Uploaded by

roopam12cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CA 1 : OOP

P O LY M O R P H I S M A N D I T S
I M P L E M E N TAT I O N

NAME ROOPAM BARMAN

ROLL NO. 34900122051

REG. NO. 223490110068 OF 2022-23

SUBJECT Object Oriented Programming


Department : Computer Science & Engineering Sem : 5th

1
CONTENTS

• What is polymorphism?

• Types of polymorphism

• Compile-Time Polymorphism in Java

• Method Overloading

• Sub-types of Compile Time Polymorphism

• Run-Time Polymorphism

• Sub-types of Run-Time Polymorphism

• Advantages of Polymorphism

• Disadvantages of Polymorphism

• Thank You

2
P O LY M O R P H I S M

The word polymorphism means having many forms. In simple words, we can define Java Polymorphism as the
ability of a message to be displayed in more than one form. In this article, we will learn what is polymorphism and
it’s type.

Real-life Illustration of Polymorphism in Java: A person at the same time can have different characteristics. Like
a man at the same time is a father, a husband, and an employee. So the same person possesses different
behaviors in different situations. This is called polymorphism.

What is Polymorphism in Java?

Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism


allows us to perform a single action in different ways. In other words, polymorphism allows you to define one
interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it
means many forms.
T Y P E S O F P O LY M O R P H I S M

In Java Polymorphism is mainly divided into two types:

• Compile-time Polymorphism

• Runtime Polymorphism
C O M P I L E - T I M E P O LY M O R P H I S M I N J A V A

It is also known as static polymorphism. This type of polymorphism is


achieved by function overloading or operator overloading.
METHOD OVERLOADING

When there are multiple functions with the same name but different parameters then these functions are said to
be overloaded. Functions can be overloaded by changes in the number of arguments or/and a change in the type of
arguments.
// Java Program for Method overloading
// By using Different Types of Arguments

// Class 1
// Helper class
class Helper {

// Method with 2 integer parameters


static int Multiply(int a, int b)
{
// Returns product of integer numbers
return a * b;
}

// Method 2
// With same name but with 2 double parameters
static double Multiply(double a, double b)
{
// Returns product of double numbers
return a * b;
}
}

// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Calling method by passing
// input as in arguments
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
}
}
S U B T Y P E S O F C O M P I L E T I M E P O LY M O R P H I S M

1. Function Overloading

It is a feature in C++ where multiple functions can have the same name but with different parameter lists.
The compiler will decide which function to call based on the number and types of arguments passed to
the function.

2. Operator Overloading

It is a feature in C++ where the operators such as +, -, *, etc. can be given additional meanings when
applied to user-defined data types.

3. Template

it is a powerful feature in C++ that allows us to write generic functions and classes. A template is a
blueprint for creating a family of functions or classes.
// Java Program for Method Overriding

R U N T I M E P O LY M O R P H I S M I N J A V A // Class 1
// Helper class
class Parent {

// Method of parent class


void Print()
{

// Print statement
System.out.println("parent class");

It is also known as Dynamic Method


}
}

// Class 2

Dispatch. It is a process in which a function


// Helper class
class subclass1 extends Parent {

// Method

call to the overridden method is resolved at }


void Print() { System.out.println("subclass1"); }

Runtime. This type of polymorphism is


// Class 3
// Helper class
class subclass2 extends Parent {

achieved by Method Overriding. Method


// Method
void Print()
{

overriding, on the other hand, occurs when


// Print statement
System.out.println("subclass2");
}
}

a derived class has a definition for one of // Class 4


// Main class

the member functions of the base class.


class GFG {

// Main driver method


public static void main(String[] args)

That base function is said to be overridden.


{

// Creating object of class 1


Parent a;

// Now we will be calling print methods


} // inside main() method

a = new subclass1();
a.Print();

a = new subclass2();
a.Print();
}
S U B T Y P E O F R U N - T I M E P O LY M O R P H I S M
i. Virtual functions

It allows an object of a derived class to behave as if it were an object of the base class. The derived class can
override the virtual function of the base class to provide its own implementation. The function call is resolved at
runtime, depending on the actual type of the object.

Polymorphism in Java is a concept that allows objects of different classes to be treated as


objects of a common class. It enables objects to behave differently based on their specific class
type.
A D V A N TA G E S O F P O LY M O R P H I S M I N J A V A

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.
D I S A D V A N TA G E S O F P O LY M O R P H I S M I N J A V A

1. Can make it more difficult to understand the behavior of an object,


especially if the code is complex.

2. This may lead to performance issues, as polymorphic behavior may


require additional computations at runtime.
• THANK YOU

You might also like