SlideShare a Scribd company logo
1 of 14
THINKING IN OBJECT-
ORIENTED
Sadad PSP, IT Dept
Winter-Spring 2019
4. POLYMORPHISM
means many and means form.
Polymorphism is the ability of an entity (e.g. variable, class, method,
object, code, parameter, etc.) to take on different meanings in different
contexts.
POLYMORPHISM CATEGORIES
: a piece of code works are finite and all
those types must be known when the code is written
 Overloading Polymorphism
 Coercion polymorphism
: a piece of code is written in such a way
that it works for infinite number of types
 Inclusion polymorphism
 Parametric polymorphism
OVERLOADING POLYMORPHISM
When a or an has at least two definitions that work
on different type
/*********************************Method Overloading**************************/
public class MathUtil {
public static int max(int n1, int n2) {
/* Code to determine the maximum of two integers goes here */
}
public static double max(double n1, double n2) {
/* Code to determine the maximum of two floating-point numbers goes here */
}
public static int max(int[] num) {
/* Code to determine the maximum of an array of int goes here */
}
}
/*************************************************client code*************************/
int max1 = MathUtil.max(10, 23); // Uses max(int, int)
double max2 = MathUtil.max(10.34, 2.89); // Uses max(double, double)
int max3 = MathUtil.max(new int[]{1, 89, 8, 3}); // Uses max(int[])
/*************Operator Overloading********************/
int n1 = 10 + 20; // Adds two integers
double n2 = 10.20 + 2.18; // Adds two floating-point numbers
String str = "Hi " + "there"; // Concatenates two string
COERCION POLYMORPHISM
When a type is implicitly converted (coerced) to another type
automatically even if it was not intended explicitly
int num = 707;
double d1 = (double)num; // Explicit conversion of int to
double double d2 = num; // Implicit conversion of int to double (coercion)
INCLUSION POLYMORPHISM
When a piece of code that is written using a type works for all its subtypes
void processDetails(Person p) {
/* Write code using the formal parameter p, which is of type Person.
The same code will work if an object of any of the subclass of Person is passed to this method. */
}
/************************************client code*************************************************/
Person p1 = new Person();
Employee e1 = new ;
Customer c1 = create a Customer object;
processDetails(p1); // Use Person type
processDetails(e1); // Use Employee type, which is a subclass of Person
processDetails(c1); // Use Customer type, which is a subclass of Person
NOTE: METHOD HIDING
Method hiding means subclass has defined a class method with the
same signature as a class method in the superclass. In that case
the method of superclass is hidden by the subclass.
PARAMETRIC POLYMORPHISM
Parametric polymorphism is achieved by using a type variable when
writing the code, rather than using any specific type
It is also called “true” polymorphism because it lets you write true
generic code that works for any types (related or unrelated)
PARAMETRIC POLYMORPHISM
TYPES
Means that you can use only the type originally specified;
so an invariant generic type parameter is neither covariant nor
contravariant.
Enables you to use a more derived type than originally
specified.
Enables you to use a more generic (less derived)
type than originally specified.
IEnumerable<Derived> d = new List<Derived>(); //IEnumerable<out T>
IEnumerable<Base> b = d;
Action<Base> b = (target) => { Console.WriteLine(target.GetType().Name); }; //Action<in T>
Action<Derived> d = b;
d(new Derived());
DISPATCH
In simple words, it is a way how the programming
language calls a method or a function.
•Static Dispatch
•Dynamic Dispatch
STATIC DISPATCH
Every method is known at the compile time
public interface IBar {}
public class Bar : IBar {}
public sealed class FooBar : Bar {}
public static class ConsolePrinter
{
public static void Print(IBar item){Console.WriteLine("IBar"); }
public static void Print(Bar item){ Console.WriteLine("Bar"); }
public static void Print(FooBar item){
Console.WriteLine("FooBar"); }
}
var bar = new Bar();
var foo = new FooBar();
IBar ibar = new FooBar();
ConsolePrinter.Print(bar);
ConsolePrinter.Print(foo);
ConsolePrinter.Print(ibar);
// prints Bar
// prints FooBar
// prints IBar, doesn’t
work
DYNAMIC DISPATCH
Dynamically dispatched methods are determined at run time based on its
parameter’s types:
Single dynamic dispatch
Multiple dynamic dispatch
SINGLE DYNAMIC DISPATCH
Overriding: Single ones use just one parameter to select a method
public class SurveyBase{
public virtual void DoSurvey(){
Console.WriteLine("Base Class");
}
}
public class Survey : SurveyBase{
public override void DoSurvey() {
Console.WriteLine("Derived Class");
}
}
/*****************************Client
Code**************************************/
SurveyBase base = new Survey();
base.DoSurvey(); // Prints "Derived Class"
MULTIPLE DYNAMIC DISPATCH
The multiple ones can take advantage of as many parameters they want
var bar = new Bar();
var foo = new FooBar();
IBar ibar = new FooBar();
IBar[] items = { bar, foo, ibar };
foreach (var item in items)
{
ConsolePrinter.Print(item);
}
// prints IBar
// prints IBar
// prints IBar
var bar = new Bar();
var foo = new FooBar();
IBar ibar = new FooBar();
IBar[] items = { bar, foo, ibar };
foreach ( item in items)
{
ConsolePrinter.Print(item);
}
// prints Bar
// prints FooBar
// prints FooBar

More Related Content

What's hot

C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 
Tutorial on c language programming
Tutorial on c language programmingTutorial on c language programming
Tutorial on c language programmingSudheer Kiran
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_outputAnil Dutt
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in CColin
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 
Basic of c language
Basic of c languageBasic of c language
Basic of c languagesunilchute1
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)nmahi96
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 

What's hot (20)

C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
Tutorial on c language programming
Tutorial on c language programmingTutorial on c language programming
Tutorial on c language programming
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
string , pointer
string , pointerstring , pointer
string , pointer
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in C
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
C language
C languageC language
C language
 
C Theory
C TheoryC Theory
C Theory
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
Basic of c language
Basic of c languageBasic of c language
Basic of c language
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
 
Learning c++
Learning c++Learning c++
Learning c++
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 

Similar to Thinking in object oriented - Part 2

Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semKavita Dagar
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
All You Need to Know About Type Script
All You Need to Know About Type ScriptAll You Need to Know About Type Script
All You Need to Know About Type ScriptFolio3 Software
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScriptAleš Najmann
 
CIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docx
CIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docxCIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docx
CIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docxclarebernice
 
Fluent Development with FLOW3 1.0
Fluent Development with FLOW3 1.0Fluent Development with FLOW3 1.0
Fluent Development with FLOW3 1.0Robert Lemke
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiMuhammed Thanveer M
 
Swift Programming
Swift ProgrammingSwift Programming
Swift ProgrammingCodemotion
 
Virtual function
Virtual functionVirtual function
Virtual functionharman kaur
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 

Similar to Thinking in object oriented - Part 2 (20)

2.dynamic
2.dynamic2.dynamic
2.dynamic
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
All You Need to Know About Type Script
All You Need to Know About Type ScriptAll You Need to Know About Type Script
All You Need to Know About Type Script
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
 
CIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docx
CIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docxCIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docx
CIS355A_StudentName_CourseProjectCIS355A Week 7 Course Project..docx
 
Fluent Development with FLOW3 1.0
Fluent Development with FLOW3 1.0Fluent Development with FLOW3 1.0
Fluent Development with FLOW3 1.0
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
 
Swift Programming
Swift ProgrammingSwift Programming
Swift Programming
 
Virtual function
Virtual functionVirtual function
Virtual function
 
oops.pptx
oops.pptxoops.pptx
oops.pptx
 
C++ language
C++ languageC++ language
C++ language
 
function.pptx
function.pptxfunction.pptx
function.pptx
 
C programming
C programmingC programming
C programming
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Clean code slide
Clean code slideClean code slide
Clean code slide
 

Recently uploaded

Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Eraconfluent
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)Roberto Bettazzoni
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2WSO2
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 

Recently uploaded (20)

Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 

Thinking in object oriented - Part 2

  • 1. THINKING IN OBJECT- ORIENTED Sadad PSP, IT Dept Winter-Spring 2019
  • 2. 4. POLYMORPHISM means many and means form. Polymorphism is the ability of an entity (e.g. variable, class, method, object, code, parameter, etc.) to take on different meanings in different contexts.
  • 3. POLYMORPHISM CATEGORIES : a piece of code works are finite and all those types must be known when the code is written  Overloading Polymorphism  Coercion polymorphism : a piece of code is written in such a way that it works for infinite number of types  Inclusion polymorphism  Parametric polymorphism
  • 4. OVERLOADING POLYMORPHISM When a or an has at least two definitions that work on different type /*********************************Method Overloading**************************/ public class MathUtil { public static int max(int n1, int n2) { /* Code to determine the maximum of two integers goes here */ } public static double max(double n1, double n2) { /* Code to determine the maximum of two floating-point numbers goes here */ } public static int max(int[] num) { /* Code to determine the maximum of an array of int goes here */ } } /*************************************************client code*************************/ int max1 = MathUtil.max(10, 23); // Uses max(int, int) double max2 = MathUtil.max(10.34, 2.89); // Uses max(double, double) int max3 = MathUtil.max(new int[]{1, 89, 8, 3}); // Uses max(int[]) /*************Operator Overloading********************/ int n1 = 10 + 20; // Adds two integers double n2 = 10.20 + 2.18; // Adds two floating-point numbers String str = "Hi " + "there"; // Concatenates two string
  • 5. COERCION POLYMORPHISM When a type is implicitly converted (coerced) to another type automatically even if it was not intended explicitly int num = 707; double d1 = (double)num; // Explicit conversion of int to double double d2 = num; // Implicit conversion of int to double (coercion)
  • 6. INCLUSION POLYMORPHISM When a piece of code that is written using a type works for all its subtypes void processDetails(Person p) { /* Write code using the formal parameter p, which is of type Person. The same code will work if an object of any of the subclass of Person is passed to this method. */ } /************************************client code*************************************************/ Person p1 = new Person(); Employee e1 = new ; Customer c1 = create a Customer object; processDetails(p1); // Use Person type processDetails(e1); // Use Employee type, which is a subclass of Person processDetails(c1); // Use Customer type, which is a subclass of Person
  • 7. NOTE: METHOD HIDING Method hiding means subclass has defined a class method with the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass.
  • 8. PARAMETRIC POLYMORPHISM Parametric polymorphism is achieved by using a type variable when writing the code, rather than using any specific type It is also called “true” polymorphism because it lets you write true generic code that works for any types (related or unrelated)
  • 9. PARAMETRIC POLYMORPHISM TYPES Means that you can use only the type originally specified; so an invariant generic type parameter is neither covariant nor contravariant. Enables you to use a more derived type than originally specified. Enables you to use a more generic (less derived) type than originally specified. IEnumerable<Derived> d = new List<Derived>(); //IEnumerable<out T> IEnumerable<Base> b = d; Action<Base> b = (target) => { Console.WriteLine(target.GetType().Name); }; //Action<in T> Action<Derived> d = b; d(new Derived());
  • 10. DISPATCH In simple words, it is a way how the programming language calls a method or a function. •Static Dispatch •Dynamic Dispatch
  • 11. STATIC DISPATCH Every method is known at the compile time public interface IBar {} public class Bar : IBar {} public sealed class FooBar : Bar {} public static class ConsolePrinter { public static void Print(IBar item){Console.WriteLine("IBar"); } public static void Print(Bar item){ Console.WriteLine("Bar"); } public static void Print(FooBar item){ Console.WriteLine("FooBar"); } } var bar = new Bar(); var foo = new FooBar(); IBar ibar = new FooBar(); ConsolePrinter.Print(bar); ConsolePrinter.Print(foo); ConsolePrinter.Print(ibar); // prints Bar // prints FooBar // prints IBar, doesn’t work
  • 12. DYNAMIC DISPATCH Dynamically dispatched methods are determined at run time based on its parameter’s types: Single dynamic dispatch Multiple dynamic dispatch
  • 13. SINGLE DYNAMIC DISPATCH Overriding: Single ones use just one parameter to select a method public class SurveyBase{ public virtual void DoSurvey(){ Console.WriteLine("Base Class"); } } public class Survey : SurveyBase{ public override void DoSurvey() { Console.WriteLine("Derived Class"); } } /*****************************Client Code**************************************/ SurveyBase base = new Survey(); base.DoSurvey(); // Prints "Derived Class"
  • 14. MULTIPLE DYNAMIC DISPATCH The multiple ones can take advantage of as many parameters they want var bar = new Bar(); var foo = new FooBar(); IBar ibar = new FooBar(); IBar[] items = { bar, foo, ibar }; foreach (var item in items) { ConsolePrinter.Print(item); } // prints IBar // prints IBar // prints IBar var bar = new Bar(); var foo = new FooBar(); IBar ibar = new FooBar(); IBar[] items = { bar, foo, ibar }; foreach ( item in items) { ConsolePrinter.Print(item); } // prints Bar // prints FooBar // prints FooBar

Editor's Notes

  1. When one task is performed by different ways i.e. known as polymorphism. For example: to convenes the customer differently, to draw something e.g. shape or rectangle etc.
  2. Visitor Pattern