[go: up one dir, main page]

0% found this document useful (0 votes)
140 views17 pages

Presentation On Polymorphism by Rojip Rai

Polymorphism allows objects of different subclasses to be treated as objects of their parent class. There are two types of polymorphism in Java: compile-time polymorphism through method overloading, and runtime polymorphism through method overriding. Polymorphism provides advantages like code reusability, extensibility, and alignment with real world concepts, while also raising some performance and readability issues. It is needed to achieve software reuse and facilitate software maintenance.

Uploaded by

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

Presentation On Polymorphism by Rojip Rai

Polymorphism allows objects of different subclasses to be treated as objects of their parent class. There are two types of polymorphism in Java: compile-time polymorphism through method overloading, and runtime polymorphism through method overriding. Polymorphism provides advantages like code reusability, extensibility, and alignment with real world concepts, while also raising some performance and readability issues. It is needed to achieve software reuse and facilitate software maintenance.

Uploaded by

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

OOPJ

Presentation on Polymorphism

Prepared By:
Rozeep Rai (C30105210060)
Contents

• What is polymorphism?

• Example of polymorphism

• Types of polymorphism

• Advantages Of Polymorphism

• Need of polymorphism

• References
What is polymorphism?

• Polymorphism means "many forms", and it occurs when we have many classes that
are related to each other by inheritance.Java is an object-oriented programming
language that supports the concept of polymorphism.

• Alternatively, it is defined as the ability of a reference variable to change


behavior according to what object instance is holding.
• This allows multiple objects of different subclasses to be treated as objects as a
single parent class, while automatically selecting the proper methods to apply to
an object based on the child class it belongs.
Characteristics of Polymorphism
Following are the significant characteristics of Polymorphism in Java:

• The functionality of a method behaves differently in different


scenarios.
• The behavior of a method depends on the data provided.
• It allows the same name for a member or method in a class with
different types.
• Polymorphism supports implicit type conversion.
Peter Parker Is Polymorphic

• As a high school student, Peter goes


to school and hangs out with friends.
As Spider Man, he bashes up
baddies. Peter has multiple
behaviors based on the context.
Peter is polymorphic.
Two types of polymorphism

Compile -Time
Polymorphism

Polymorphism In
Java

Run-Time
Polymorphism
1. Method Overloading in Java :
This is an example of compile time (or static polymorphism)

2. Method Overriding in Java :


This is an example of runtime time (or dynamic polymorphism)
• Compile-time polymorphism refers to behaviour that is
resolved when your Java class is compiled.

• Method overloading is an example of compile-time


polymorphism. Method overloading is how you can use
method with same name to do different things based on the
parameters passed.

A calculator can add 2 integers. It can also


add 2 floats. The addition method adds
differently based on the inputs.
• Compile time Polymorphism (or Static polymorphism) :
Polymorphism that is resolved during compiler time is known as static polymorphism. Method
overloading is an example of compile time polymorphism. Method Overloading: This allows us to
have more than one method having the same name, if the parameters of methods are different in
number, sequence and data types of parameters.

• Runtime Polymorphism (or Dynamic polymorphism) :


It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to
an overridden method is resolved at runtime, thats why it is called runtime polymorphism. I
Compile time Polymorphism
class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
}
Runtime Polymorphism
class ABC{
public void myMethod(){
System.out.println("Overridden Method");
}
}
public class XYZ extends ABC{
public void myMethod(){
System.out.println("Overriding Method");
}
public static void main(String args[]){
ABC obj = new XYZ();
obj.myMethod();
}
Advantages Of Polymorphism

Code Ease Of
Cleanliness Implementation

Overloaded
Aligned With
Real World Constructors

Reusability And
Extensibility
Disadvantages Of Polymorphism

• Polymorphism ends up raising performance issues in real-


time
• Polymorphism reduces the readability of the code
• Programmers find Polymorphism a little challenging to
implement
Why do we need polymorphism

• To achieve software reuse


• Facilitate software maintenance and upgrading
Compile-time polymorphism vs Runtime polymorphism

Compile time Run time

• Run time polymorphism where at run time we came to


• Compile time polymorphism means binding is know which method is going to invoke
occuring at compile time
• It can be achieved through dynamic binding
• It can be achieved through static binding
• Inheritance is involved
• Inheritance is not involved
• Method overriding is an example of runtime
• Method overloading is  an example of compile time polymorphism
polymorphism
References

• https://www.slideserve.com/cahil/java-212-inheritance-and-polymorphism/?
utm_source=slideserve&utm_medium=website&utm_campaign=auto+related+l
oad
• 3schools.com/java/java_polymorphism.asp
• https://www.simplilearn.com/tutorials/java-tutorial/java-polymorphism

You might also like