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

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Arif Ansari
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
Spotle.ai
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
sureshraj43
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Raffaele Doti
 
Python set
Python setPython set
Python set
Mohammed Sikander
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
Java2Blog
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
Meghaj Mallick
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
python Function
python Function python Function
python Function
Ronak Rathi
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 

What's hot (20)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Python set
Python setPython set
Python set
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
Operator Overloading & Function Overloading
Operator Overloading & Function OverloadingOperator Overloading & Function Overloading
Operator Overloading & Function Overloading
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
python Function
python Function python Function
python Function
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 

Similar to OOP - Polymorphism

Polymorphism OOP new Gate.pptx
Polymorphism OOP new Gate.pptxPolymorphism OOP new Gate.pptx
Polymorphism OOP new Gate.pptx
AssadLeo1
 
Polymorphism OOP Old Gate.pptx
Polymorphism OOP Old Gate.pptxPolymorphism OOP Old Gate.pptx
Polymorphism OOP Old Gate.pptx
AssadLeo1
 
Chapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdfChapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdf
MornThea
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
talha ijaz
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
Mahi Mca
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Ducat India
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
karthikenlume
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
Oop lecture 06
Oop lecture 06Oop lecture 06
Oop lecture 06
University of Chitral
 
Java session2
Java session2Java session2
Java session2
Rajeev Kumar
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
Ayush Gupta
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Nadeesha Thilakarathne
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
ShuvrojitMajumder
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdf
Datacademy.ai
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
AshutoshTrivedi30
 
28csharp
28csharp28csharp
28csharp
Sireesh K
 
28c
28c28c
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
smumbahelp
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
SURBHI SAROHA
 

Similar to OOP - Polymorphism (20)

Polymorphism OOP new Gate.pptx
Polymorphism OOP new Gate.pptxPolymorphism OOP new Gate.pptx
Polymorphism OOP new Gate.pptx
 
Polymorphism OOP Old Gate.pptx
Polymorphism OOP Old Gate.pptxPolymorphism OOP Old Gate.pptx
Polymorphism OOP Old Gate.pptx
 
Chapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdfChapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdf
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
 
Oop lecture 06
Oop lecture 06Oop lecture 06
Oop lecture 06
 
Java session2
Java session2Java session2
Java session2
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdf
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
28csharp
28csharp28csharp
28csharp
 
28c
28c28c
28c
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 

More from Mudasir Qazi

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

More from Mudasir Qazi (14)

Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Database - SQL Joins
Database - SQL JoinsDatabase - SQL Joins
Database - SQL Joins
 
Database - Normalization
Database - NormalizationDatabase - Normalization
Database - Normalization
 
Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)
 
OOP - Understanding association, aggregation, composition and dependency
OOP - Understanding association, aggregation, composition and dependencyOOP - Understanding association, aggregation, composition and dependency
OOP - Understanding association, aggregation, composition and dependency
 
OOP - Java is pass-by-value
OOP - Java is pass-by-valueOOP - Java is pass-by-value
OOP - Java is pass-by-value
 
OOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPOOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOP
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - Introduction
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade Pattern
 
Design Pattern - Chain of Responsibility
Design Pattern - Chain of ResponsibilityDesign Pattern - Chain of Responsibility
Design Pattern - Chain of Responsibility
 

Recently uploaded

一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Transcat
 
AI-Based Home Security System : Home security
AI-Based Home Security System : Home securityAI-Based Home Security System : Home security
AI-Based Home Security System : Home security
AIRCC Publishing Corporation
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
AnasAhmadNoor
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
ijseajournal
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
Kamal Acharya
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
Indrajeet sahu
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
vmspraneeth
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf
AlvianRamadhani5
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 

Recently uploaded (20)

一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
 
AI-Based Home Security System : Home security
AI-Based Home Security System : Home securityAI-Based Home Security System : Home security
AI-Based Home Security System : Home security
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 

OOP - Polymorphism

  • 1. Polymorphism Definition, types and implementation 22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 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 - mudasirqazi00@gmail.com 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 - mudasirqazi00@gmail.com 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 - mudasirqazi00@gmail.com 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 - mudasirqazi00@gmail.com 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 - 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.
  • 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 - 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.
  • 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 - mudasirqazi00@gmail.com 8
  • 9. 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.
  • 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 - mudasirqazi00@gmail.com 10
  • 11. 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.
  • 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 - mudasirqazi00@gmail.com 12
  • 13. Casting in Polymorphism (2) 22-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 13