SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Circle

Rectangle

Triangle

Draw

Bhushan Mulmule
bhushan.mulmule@gmail.com

www.dotnetvideotutorial.com
Polymorphism is
Ability of different objects to respond same message
in different ways.

Circle

Rectangle

Triangle

Draw
We can achieve it using…
Inheritance, Upcasting and Late Binding
or

Abstract Class Implementation
or

Interface Implementation
Let us focus on first technique in this session

Polymorphism using Inheritance, Upcasting
and Late Binding

www.dotnetvideotutorial.com
Upcasting is holding derived class object
in base class reference
If we have this class hierarchy
we generally create objects…

www.dotnetvideotutorial.com
where references and objects are of same types

Shape s = new Shape();

s
shape

Circle c = new Circle();

c
Circle

Rectangle r = new Rectangle();

shape

Circle

r
Rectangle

Rectangle
As Circle and Rectangle are derived from Shape class.
You can also create object using upcasting…
where base class reference can hold
objects of derived class

Shape s = new Shape();

s
shape

shape

s = new Circle();
Circle

s = new Rectangle();

Rectangle

www.dotnetvideotutorial.com
Now let us try to understand Late Binding
Binding is connecting method Invocation to method
implementation
By default .NET use Early Binding …

Early binding is also referred as Static Binding or Compile Time Binding
If we have Draw() method in base class and also
redefined it in derived classes

www.dotnetvideotutorial.com
Redefining Draw() method
in Circle and Rectangle
Class hides the base class
implementation of Draw().
This is knows as
Shadowing.

Note: new keyword just confirms that you are
intentionally shadowing Draw() method in
derived class.
And we have created objects using upcasting
and invoked Draw() method
Then the output that we are expecting is…
Expected Output

www.dotnetvideotutorial.com
But What’s this?
Output is surprising…
Actual Output

www.dotnetvideotutorial.com
Why?

www.dotnetvideotutorial.com
Because by default .NET use early binding.
Compiler simply checks type of reference and bind it
accordingly.
Compiler doesn't care about type of object. Actually there are no objects in
memory at compile time
As s is of Shape type so all the three calls will get binded to
Draw() of Shape class irrespective of Object it is pointing to
What's the solution?

Is it possible to bind method call depending on object type instead
of reference type?
And here is the magic keyword

virtual
Call to virtual method gets resolved at runtime depending on actual
object type.
The virtual keyword marks method for late
or dynamic binding.

Virtual method has to be overridden in a
derived class using override keyword and
can be modified
Now Let us run same client code

www.dotnetvideotutorial.com
Will call Draw() of Shape

Will call Draw() of Circle

Will call Draw() of Rectangle
And yes this time output will be…
As expected.

www.dotnetvideotutorial.com
Polymorphism is
ability of different objects

to respond same message
In different ways
For Video visit
www.dotnetvideotutorial.com
Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com

Weitere ähnliche Inhalte

Was ist angesagt?

Rakesh Bijawat , BCA Third Year
Rakesh Bijawat , BCA Third YearRakesh Bijawat , BCA Third Year
Rakesh Bijawat , BCA Third YearDezyneecole
 
Rakesh Bijawat, BCA 2nd Year
Rakesh Bijawat, BCA 2nd YearRakesh Bijawat, BCA 2nd Year
Rakesh Bijawat, BCA 2nd Yeardezyneecole
 
Karishma Dhanwani,BCA 2nd Year
Karishma Dhanwani,BCA 2nd YearKarishma Dhanwani,BCA 2nd Year
Karishma Dhanwani,BCA 2nd Yeardezyneecole
 
Pooja Sharma ,BCA 2nd Year
Pooja Sharma ,BCA 2nd YearPooja Sharma ,BCA 2nd Year
Pooja Sharma ,BCA 2nd Yeardezyneecole
 
Rounit Mathur,BCA 2nd year.
Rounit Mathur,BCA 2nd year.Rounit Mathur,BCA 2nd year.
Rounit Mathur,BCA 2nd year.dezyneecole
 
Deepika Mittal,BCA ,2nd Year
Deepika Mittal,BCA ,2nd Year Deepika Mittal,BCA ,2nd Year
Deepika Mittal,BCA ,2nd Year dezyneecole
 
Kaushal Soni,BCA,2nd Year
Kaushal Soni,BCA,2nd YearKaushal Soni,BCA,2nd Year
Kaushal Soni,BCA,2nd Yeardezyneecole
 
Nikita Totlani ,BCA 2nd year
Nikita Totlani ,BCA 2nd year Nikita Totlani ,BCA 2nd year
Nikita Totlani ,BCA 2nd year dezyneecole
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Yeardezyneecole
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And EnumsBhushan Mulmule
 
Aanchal Gupta,BCA 2nd year
Aanchal Gupta,BCA 2nd yearAanchal Gupta,BCA 2nd year
Aanchal Gupta,BCA 2nd yeardezyneecole
 

Was ist angesagt? (11)

Rakesh Bijawat , BCA Third Year
Rakesh Bijawat , BCA Third YearRakesh Bijawat , BCA Third Year
Rakesh Bijawat , BCA Third Year
 
Rakesh Bijawat, BCA 2nd Year
Rakesh Bijawat, BCA 2nd YearRakesh Bijawat, BCA 2nd Year
Rakesh Bijawat, BCA 2nd Year
 
Karishma Dhanwani,BCA 2nd Year
Karishma Dhanwani,BCA 2nd YearKarishma Dhanwani,BCA 2nd Year
Karishma Dhanwani,BCA 2nd Year
 
Pooja Sharma ,BCA 2nd Year
Pooja Sharma ,BCA 2nd YearPooja Sharma ,BCA 2nd Year
Pooja Sharma ,BCA 2nd Year
 
Rounit Mathur,BCA 2nd year.
Rounit Mathur,BCA 2nd year.Rounit Mathur,BCA 2nd year.
Rounit Mathur,BCA 2nd year.
 
Deepika Mittal,BCA ,2nd Year
Deepika Mittal,BCA ,2nd Year Deepika Mittal,BCA ,2nd Year
Deepika Mittal,BCA ,2nd Year
 
Kaushal Soni,BCA,2nd Year
Kaushal Soni,BCA,2nd YearKaushal Soni,BCA,2nd Year
Kaushal Soni,BCA,2nd Year
 
Nikita Totlani ,BCA 2nd year
Nikita Totlani ,BCA 2nd year Nikita Totlani ,BCA 2nd year
Nikita Totlani ,BCA 2nd year
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
 
Aanchal Gupta,BCA 2nd year
Aanchal Gupta,BCA 2nd yearAanchal Gupta,BCA 2nd year
Aanchal Gupta,BCA 2nd year
 

Ähnlich wie Polymorphism

Ähnlich wie Polymorphism (20)

Spring tutorial - dependency injection
Spring tutorial - dependency injectionSpring tutorial - dependency injection
Spring tutorial - dependency injection
 
Abstract
AbstractAbstract
Abstract
 
L4
L4L4
L4
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
MVC Puree - Approaches to MVC with Umbraco
MVC Puree - Approaches to MVC with UmbracoMVC Puree - Approaches to MVC with Umbraco
MVC Puree - Approaches to MVC with Umbraco
 
Contoh Factory pattern
Contoh Factory patternContoh Factory pattern
Contoh Factory pattern
 
Classes1
Classes1Classes1
Classes1
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 
Classes2
Classes2Classes2
Classes2
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Reflection
ReflectionReflection
Reflection
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 
Oop
OopOop
Oop
 
Java script unleashed
Java script unleashedJava script unleashed
Java script unleashed
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Highly Strung
Highly StrungHighly Strung
Highly Strung
 

Mehr von Bhushan Mulmule

Implementing auto complete using JQuery
Implementing auto complete using JQueryImplementing auto complete using JQuery
Implementing auto complete using JQueryBhushan Mulmule
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Bhushan Mulmule
 
Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Bhushan Mulmule
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Bhushan Mulmule
 
Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2Bhushan Mulmule
 
Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Bhushan Mulmule
 
Dependency injection for beginners
Dependency injection for beginnersDependency injection for beginners
Dependency injection for beginnersBhushan Mulmule
 
Understanding Interfaces
Understanding InterfacesUnderstanding Interfaces
Understanding InterfacesBhushan Mulmule
 
Getting started with C# Programming
Getting started with C# ProgrammingGetting started with C# Programming
Getting started with C# ProgrammingBhushan Mulmule
 
Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Bhushan Mulmule
 

Mehr von Bhushan Mulmule (15)

Implementing auto complete using JQuery
Implementing auto complete using JQueryImplementing auto complete using JQuery
Implementing auto complete using JQuery
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5
 
Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3
 
Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2Windows Forms For Beginners Part - 2
Windows Forms For Beginners Part - 2
 
Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1
 
NInject - DI Container
NInject - DI ContainerNInject - DI Container
NInject - DI Container
 
Dependency injection for beginners
Dependency injection for beginnersDependency injection for beginners
Dependency injection for beginners
 
Understanding Interfaces
Understanding InterfacesUnderstanding Interfaces
Understanding Interfaces
 
Inheritance
InheritanceInheritance
Inheritance
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Methods
MethodsMethods
Methods
 
Flow Control (C#)
Flow Control (C#)Flow Control (C#)
Flow Control (C#)
 
Getting started with C# Programming
Getting started with C# ProgrammingGetting started with C# Programming
Getting started with C# Programming
 
Overview of .Net Framework 4.5
Overview of .Net Framework 4.5Overview of .Net Framework 4.5
Overview of .Net Framework 4.5
 

Kürzlich hochgeladen

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 

Polymorphism