SlideShare a Scribd company logo
Polymorphism
Definition, types and implementation
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 1
Contents / Agenda
• Definition and Types
• Ad-hoc polymorphism
• Implementation of all types of ad-hoc polymorphism
• Parametric Polymorphism
• Implementation of parametric polymorphism
• Subtyping polymorphism
• Implementation of Subtyping polymorphism
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 2
Definition
• Polymorphism is an object-oriented programming concept that refers
to the ability of a variable, function or object to take on multiple
forms. A language that features polymorphism allows developers to
program in the general rather than program in the specific.
• Following are the main types of polymorphism
1. Ad-hoc Polymorphism
2. Parametric Polymorphism
3. Subtyping Polymorphism
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 3
Ad-hoc Polymorphism
• It is also known as Function Overloading or Static Binding and also
Operator Overloading.
• Ad-hoc Polymorphism is a phenomenon in which you can create
multiple functions with the same name but that have different
headers or signature.
• It can be achieved using following ways
1. By changing the type of arguments
2. By changing the order of arguments
3. By changing number of arguments
• You can not change the return-type of a function in overloading.
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 4
Implementation (1)
by changing number of arguments
• You can change the number of arguments to overload a method.
A simple example is shown in the picture, where a
method “Add” is overloaded three times with same
return-type but with different number of arguments.
First method takes one argument, second take two and
third takes three arguments. You can call the Add method
with any number of arguments (from 1 to 3 of course)
compiler will choose a proper method on compile time.
As the compiler choses the proper method on compile
time that’s why it is called compile time polymorphism or
Static polymorphism.
Note: in example all arguments are of type integer but
you can give any type of argument and provide a proper
functionality. And same is the case with Constructors.
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 5
Implementation (2)
by changing type of arguments
• You can achieve this by just changing the type of arguments.
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 6
You see that, we don’t change the
number of arguments here in this
example, but we change the type
from integer to double. And by doing
this we have achieved the
polymorphism.
Implementation (3)
by changing order of arguments
• You can also just change the order of arguments to overload a method
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 7
Here in this example, there are two overloaded
method in the class. We overload them by just
changing the order of arguments.
In first method, integer is 1st argument and string is 2nd
argument but in the second method string is 1st
argument and integer is the 1st argument.
Parametric Polymorphism
• It is also known as Template Polymorphism or Late/dynamic Binding.
• Parametric polymorphism allows a function or a data type to be written
generically, so that it can handle values identically without depending on
their type. Parametric polymorphism is a way to make a language more
expressive, while still maintaining full static type-safety.
• The concept of parametric polymorphism applies to both data types and
functions. A function that can evaluate to or be applied to values of
different types is known as a polymorphic function. A data type that can
appear to be of a generalized type (e.g., a list with elements of arbitrary
type) is designated polymorphic data type like the generalized type from
which such specializations are made.
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 8
Implementation
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 9
A generic function swap is shown in the picture.
This method can work with any type you will
provide in angle brackets even with strings and
complex types.
I have created integers and characters in this
example and same function will swap both type of
variables.
This is parametric polymorphism and swap
function is polymorphic function.
Subtyping Polymorphism
• It is also known as Method Overriding or Late Binding or Dynamic
Binding or Inclusion Polymorphism.
• Method Overriding is when a method defined in a superclass or
interface is re-defined by one of its subclasses, thus
modifying/replacing the behavior the superclass provides. The
decision to call an implementation or another is dynamically taken at
runtime, depending on the object the operation is called from. Notice
the signature of the method remains the same when overriding.
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 10
Implementation
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 11
The picture shows the implementation of method overriding or
subtyping polymorphism.
Abstract class has a method that is re-defined in each concrete class
with respect to their requirements with same signature.
In the main method, when we create objects of concrete classes and
call there talk method we get different output as implemented.
letsHear(new Cat()) => Meow!
letsHear(new Dog()) => Woof!
The abstract class’s method is overridden on runtime, the compiler sees
which object is calling the method, if that method is available in same
class then it would be called, if not, then super class’s method would be
called.
Casting in Polymorphism (1)
• Two types of castings are done in polymorphism
1. Up Casting
2. Down Casting
• Up-casting is casting to a supertype, while Down-casting is casting to a subtype. Supercasting is always
allowed, but subcasting involves a type check and can throw a ClassCastException. A cast from from Dog to
an Animal is a upcast, because a Dog is-a Animal. In general, you can upcast whenever there is an is-a
relationship between two classes. Downcasting would be something like this:
• Animal animal = new Dog();
• Dog castedDog = (Dog) animal;
• Basically what you're doing is telling the program that you know what the runtime type of the object really
is. The program will do the conversion, but will still do a sanity check to make sure that it's possible. In this
case, the cast is possible because at runtime animal is actually a Dog even though the static type of animal is
Animal. However, if you were to do this:
• Animal animal = new Animal();
• Dog dog = (Dog) animal;
• You'd get a ClassCastException. The reason why is because animal's runtime type is Animal, and so when you
tell the runtime to perform the cast it sees that animal isn't really a Dog and so throws a ClassCastException.
To call a superclass's method you can do super.method() or be performing the upcast. To call a subclass's
method you have to do a downcast and risk the ClassCastException.
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 12
Casting in Polymorphism (2)
22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 13

More Related Content

What's hot (20)

PPTX
Interface in java
PhD Research Scholar
 
PPTX
Python Exception Handling
Megha V
 
PDF
Function arguments In Python
Amit Upadhyay
 
PPT
Strings
Mitali Chugh
 
PDF
Methods in Java
Jussi Pohjolainen
 
PDF
Arrays in Java
Naz Abdalla
 
PDF
Polymorphism In Java
Spotle.ai
 
PPTX
Python variables and data types.pptx
AkshayAggarwal79
 
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
PPTX
Polymorphism in Python
Home
 
PPTX
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PDF
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
PPTX
File Handling Python
Akhil Kaushik
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
polymorphism
Imtiaz Hussain
 
PPT
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
Interface in java
PhD Research Scholar
 
Python Exception Handling
Megha V
 
Function arguments In Python
Amit Upadhyay
 
Strings
Mitali Chugh
 
Methods in Java
Jussi Pohjolainen
 
Arrays in Java
Naz Abdalla
 
Polymorphism In Java
Spotle.ai
 
Python variables and data types.pptx
AkshayAggarwal79
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Polymorphism in Python
Home
 
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
File Handling Python
Akhil Kaushik
 
Python Modules
Nitin Reddy Katkam
 
polymorphism
Imtiaz Hussain
 
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
Wrapper classes
Ravi_Kant_Sahu
 
Chapter 05 classes and objects
Praveen M Jigajinni
 

Similar to OOP - Polymorphism (20)

PPTX
Polymorphism OOP new Gate.pptx
AssadLeo1
 
PPTX
Polymorphism OOP Old Gate.pptx
AssadLeo1
 
PDF
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
PDF
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
PDF
Object Oriented Programming - 7.2. Polymorphism
AndiNurkholis1
 
PPTX
Polymorphism in java
Janu Jahnavi
 
PPTX
Polymorphism
CME
 
PPTX
java poly ppt.pptx
sukhpreetsingh295239
 
PPTX
Polymorphism in java
sureshraj43
 
PPTX
Polymorphism.pptx
RiturajJain8
 
PPTX
Thinking in object oriented - Part 2
Reza Taroosheh
 
PPTX
‫‫Chapter4 Polymorphism
Mahmoud Alfarra
 
PPTX
Chapter 4
siragezeynu
 
PPTX
Java(Polymorphism)
harsh kothari
 
PDF
Object-Oriented Polymorphism Unleashed
Naresh Chintalcheru
 
PPTX
P.7 media 2 polymorphism
ahmadmuzaqqi
 
PPTX
Java polymorphism
Sujit Kumar
 
PPTX
20.5 Java polymorphism
Intro C# Book
 
PPTX
Chapter8:Understanding Polymorphism
It Academy
 
Polymorphism OOP new Gate.pptx
AssadLeo1
 
Polymorphism OOP Old Gate.pptx
AssadLeo1
 
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
Object Oriented Programming - 7.2. Polymorphism
AndiNurkholis1
 
Polymorphism in java
Janu Jahnavi
 
Polymorphism
CME
 
java poly ppt.pptx
sukhpreetsingh295239
 
Polymorphism in java
sureshraj43
 
Polymorphism.pptx
RiturajJain8
 
Thinking in object oriented - Part 2
Reza Taroosheh
 
‫‫Chapter4 Polymorphism
Mahmoud Alfarra
 
Chapter 4
siragezeynu
 
Java(Polymorphism)
harsh kothari
 
Object-Oriented Polymorphism Unleashed
Naresh Chintalcheru
 
P.7 media 2 polymorphism
ahmadmuzaqqi
 
Java polymorphism
Sujit Kumar
 
20.5 Java polymorphism
Intro C# Book
 
Chapter8:Understanding Polymorphism
It Academy
 
Ad

More from Mudasir Qazi (14)

PPTX
Design Patterns - Abstract Factory Pattern
Mudasir Qazi
 
PPTX
Database - SQL Joins
Mudasir Qazi
 
PPTX
Database - Normalization
Mudasir Qazi
 
PPTX
Database - Entity Relationship Diagram (ERD)
Mudasir Qazi
 
PPTX
OOP - Understanding association, aggregation, composition and dependency
Mudasir Qazi
 
PPTX
OOP - Java is pass-by-value
Mudasir Qazi
 
PPTX
OOP - Benefits and advantages of OOP
Mudasir Qazi
 
PPTX
Design Pattern - Singleton Pattern
Mudasir Qazi
 
PPTX
Design Pattern - Observer Pattern
Mudasir Qazi
 
PPTX
Design Pattern - MVC, MVP and MVVM
Mudasir Qazi
 
PPTX
Design Pattern - Introduction
Mudasir Qazi
 
PPTX
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
PPTX
Design pattern - Facade Pattern
Mudasir Qazi
 
PPTX
Design Pattern - Chain of Responsibility
Mudasir Qazi
 
Design Patterns - Abstract Factory Pattern
Mudasir Qazi
 
Database - SQL Joins
Mudasir Qazi
 
Database - Normalization
Mudasir Qazi
 
Database - Entity Relationship Diagram (ERD)
Mudasir Qazi
 
OOP - Understanding association, aggregation, composition and dependency
Mudasir Qazi
 
OOP - Java is pass-by-value
Mudasir Qazi
 
OOP - Benefits and advantages of OOP
Mudasir Qazi
 
Design Pattern - Singleton Pattern
Mudasir Qazi
 
Design Pattern - Observer Pattern
Mudasir Qazi
 
Design Pattern - MVC, MVP and MVVM
Mudasir Qazi
 
Design Pattern - Introduction
Mudasir Qazi
 
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
Design pattern - Facade Pattern
Mudasir Qazi
 
Design Pattern - Chain of Responsibility
Mudasir Qazi
 
Ad

Recently uploaded (20)

PPTX
UNIT III CONTROL OF PARTICULATE CONTAMINANTS
sundharamm
 
PDF
Natural Language processing and web deigning notes
AnithaSakthivel3
 
PPTX
ENSA_Module_8.pptx_nice_ipsec_presentation
RanaMukherjee24
 
PDF
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
PPT
IISM Presentation.ppt Construction safety
lovingrkn
 
PPTX
ENG8 Q1, WEEK 4.pptxoooiioooooooooooooooooooooooooo
chubbychubz1
 
PDF
A NEW FAMILY OF OPTICALLY CONTROLLED LOGIC GATES USING NAPHTHOPYRAN MOLECULE
ijoejnl
 
PPTX
Abstract Data Types (ADTs) in Data Structures
mwaslam2303
 
PPTX
GitHub_Copilot_Basics...........................pptx
ssusera13041
 
PPTX
Dolphin_Conservation_AI_txhasvssbxbanvgdghng
jeeaspirant2026fr
 
PPT
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
PPTX
Cyclic_Redundancy_Check_Presentation.pptx
alhjranyblalhmwdbdal
 
PDF
Non Text Magic Studio Magic Design for Presentations L&P.pdf
rajpal7872
 
PPTX
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
PDF
An Evaluative Study on Performance Growth Plan of ICICI Mutual Fund and SBI M...
PoonamKilaniya
 
PDF
Comparative Analysis of the Use of Iron Ore Concentrate with Different Binder...
msejjournal
 
PDF
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
PDF
1_ISO Certifications by Indian Industrial Standards Organisation.pdf
muhammad2010960
 
PPTX
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
PPT
Hazard identification and risk assessment PPT
SUNILARORA51
 
UNIT III CONTROL OF PARTICULATE CONTAMINANTS
sundharamm
 
Natural Language processing and web deigning notes
AnithaSakthivel3
 
ENSA_Module_8.pptx_nice_ipsec_presentation
RanaMukherjee24
 
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
IISM Presentation.ppt Construction safety
lovingrkn
 
ENG8 Q1, WEEK 4.pptxoooiioooooooooooooooooooooooooo
chubbychubz1
 
A NEW FAMILY OF OPTICALLY CONTROLLED LOGIC GATES USING NAPHTHOPYRAN MOLECULE
ijoejnl
 
Abstract Data Types (ADTs) in Data Structures
mwaslam2303
 
GitHub_Copilot_Basics...........................pptx
ssusera13041
 
Dolphin_Conservation_AI_txhasvssbxbanvgdghng
jeeaspirant2026fr
 
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
Cyclic_Redundancy_Check_Presentation.pptx
alhjranyblalhmwdbdal
 
Non Text Magic Studio Magic Design for Presentations L&P.pdf
rajpal7872
 
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
An Evaluative Study on Performance Growth Plan of ICICI Mutual Fund and SBI M...
PoonamKilaniya
 
Comparative Analysis of the Use of Iron Ore Concentrate with Different Binder...
msejjournal
 
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
1_ISO Certifications by Indian Industrial Standards Organisation.pdf
muhammad2010960
 
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
Hazard identification and risk assessment PPT
SUNILARORA51
 

OOP - Polymorphism

  • 1. Polymorphism Definition, types and implementation 22-Dec-14 Mudasir Qazi - [email protected] 1
  • 2. Contents / Agenda • Definition and Types • Ad-hoc polymorphism • Implementation of all types of ad-hoc polymorphism • Parametric Polymorphism • Implementation of parametric polymorphism • Subtyping polymorphism • Implementation of Subtyping polymorphism 22-Dec-14 Mudasir Qazi - [email protected] 2
  • 3. Definition • Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or object to take on multiple forms. A language that features polymorphism allows developers to program in the general rather than program in the specific. • Following are the main types of polymorphism 1. Ad-hoc Polymorphism 2. Parametric Polymorphism 3. Subtyping Polymorphism 22-Dec-14 Mudasir Qazi - [email protected] 3
  • 4. Ad-hoc Polymorphism • It is also known as Function Overloading or Static Binding and also Operator Overloading. • Ad-hoc Polymorphism is a phenomenon in which you can create multiple functions with the same name but that have different headers or signature. • It can be achieved using following ways 1. By changing the type of arguments 2. By changing the order of arguments 3. By changing number of arguments • You can not change the return-type of a function in overloading. 22-Dec-14 Mudasir Qazi - [email protected] 4
  • 5. Implementation (1) by changing number of arguments • You can change the number of arguments to overload a method. A simple example is shown in the picture, where a method “Add” is overloaded three times with same return-type but with different number of arguments. First method takes one argument, second take two and third takes three arguments. You can call the Add method with any number of arguments (from 1 to 3 of course) compiler will choose a proper method on compile time. As the compiler choses the proper method on compile time that’s why it is called compile time polymorphism or Static polymorphism. Note: in example all arguments are of type integer but you can give any type of argument and provide a proper functionality. And same is the case with Constructors. 22-Dec-14 Mudasir Qazi - [email protected] 5
  • 6. Implementation (2) by changing type of arguments • You can achieve this by just changing the type of arguments. 22-Dec-14 Mudasir Qazi - [email protected] 6 You see that, we don’t change the number of arguments here in this example, but we change the type from integer to double. And by doing this we have achieved the polymorphism.
  • 7. Implementation (3) by changing order of arguments • You can also just change the order of arguments to overload a method 22-Dec-14 Mudasir Qazi - [email protected] 7 Here in this example, there are two overloaded method in the class. We overload them by just changing the order of arguments. In first method, integer is 1st argument and string is 2nd argument but in the second method string is 1st argument and integer is the 1st argument.
  • 8. Parametric Polymorphism • It is also known as Template Polymorphism or Late/dynamic Binding. • Parametric polymorphism allows a function or a data type to be written generically, so that it can handle values identically without depending on their type. Parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety. • The concept of parametric polymorphism applies to both data types and functions. A function that can evaluate to or be applied to values of different types is known as a polymorphic function. A data type that can appear to be of a generalized type (e.g., a list with elements of arbitrary type) is designated polymorphic data type like the generalized type from which such specializations are made. 22-Dec-14 Mudasir Qazi - [email protected] 8
  • 9. Implementation 22-Dec-14 Mudasir Qazi - [email protected] 9 A generic function swap is shown in the picture. This method can work with any type you will provide in angle brackets even with strings and complex types. I have created integers and characters in this example and same function will swap both type of variables. This is parametric polymorphism and swap function is polymorphic function.
  • 10. Subtyping Polymorphism • It is also known as Method Overriding or Late Binding or Dynamic Binding or Inclusion Polymorphism. • Method Overriding is when a method defined in a superclass or interface is re-defined by one of its subclasses, thus modifying/replacing the behavior the superclass provides. The decision to call an implementation or another is dynamically taken at runtime, depending on the object the operation is called from. Notice the signature of the method remains the same when overriding. 22-Dec-14 Mudasir Qazi - [email protected] 10
  • 11. Implementation 22-Dec-14 Mudasir Qazi - [email protected] 11 The picture shows the implementation of method overriding or subtyping polymorphism. Abstract class has a method that is re-defined in each concrete class with respect to their requirements with same signature. In the main method, when we create objects of concrete classes and call there talk method we get different output as implemented. letsHear(new Cat()) => Meow! letsHear(new Dog()) => Woof! The abstract class’s method is overridden on runtime, the compiler sees which object is calling the method, if that method is available in same class then it would be called, if not, then super class’s method would be called.
  • 12. Casting in Polymorphism (1) • Two types of castings are done in polymorphism 1. Up Casting 2. Down Casting • Up-casting is casting to a supertype, while Down-casting is casting to a subtype. Supercasting is always allowed, but subcasting involves a type check and can throw a ClassCastException. A cast from from Dog to an Animal is a upcast, because a Dog is-a Animal. In general, you can upcast whenever there is an is-a relationship between two classes. Downcasting would be something like this: • Animal animal = new Dog(); • Dog castedDog = (Dog) animal; • Basically what you're doing is telling the program that you know what the runtime type of the object really is. The program will do the conversion, but will still do a sanity check to make sure that it's possible. In this case, the cast is possible because at runtime animal is actually a Dog even though the static type of animal is Animal. However, if you were to do this: • Animal animal = new Animal(); • Dog dog = (Dog) animal; • You'd get a ClassCastException. The reason why is because animal's runtime type is Animal, and so when you tell the runtime to perform the cast it sees that animal isn't really a Dog and so throws a ClassCastException. To call a superclass's method you can do super.method() or be performing the upcast. To call a subclass's method you have to do a downcast and risk the ClassCastException. 22-Dec-14 Mudasir Qazi - [email protected] 12
  • 13. Casting in Polymorphism (2) 22-Dec-14 Mudasir Qazi - [email protected] 13