SlideShare a Scribd company logo
1 of 8
Polymorphism


       Poly → many

Morph → shapes (“behaviors”)




        Copyright 2008 -- Walter Wesley   1
Advanced Polymorphism
•   You should already have a basic
    understanding of polymorphism.
•   But do you really understand how
    polymorphism works?
•   How does the compiler actually implement
    polymorphism?


                 Copyright 2008 -- Walter Wesley   2
Polymorphism in C++
•   Suppose you have a C++ class. It has a
    few methods, and some instance variables
    (fields).
•   In your program, you instantiate an object
    and you get and display the size in bytes of
    the object (C++ has a sizeof operator that
    provides the size of its operand).
•   You record the number of bytes.
                  Copyright 2008 -- Walter Wesley   3
•   You now change your class by placing the
Polymorphism in C++
               (continued)

•   Even though all you did was introduce the “virtual”
    keyword (in C++ the virtual keyword is used to
    specify that you want that method to behave
    polymorphically), your object has increased in size by
    4 bytes.
•   It is as if a pointer has been added to your object (4
    bytes is exactly the size of a pointer instance
    variable).
                      Copyright 2008 -- Walter Wesley        4
•   In fact, this is exactly what has occurred.
Polymorphism in C++
The added points is
 The VPTR pointer
     VTABLE entries (continued)
called the called
to a table VPTR
 are pointers to
(short for Virtual Pointer).
the VTABLE
 virtual methods.               MyClass


                  VPTR

     VTABLE                                            First
                                                       Virtual Method


                                  Second
The VTABLE entries                Virtual Method
are ordered in accordance
with the order in which
the methods are declared
with the class.


                               Copyright 2008 -- Walter Wesley          5
Polymorphism in Java
•   Polymorphism in Java is implemented in
    exactly the same way as in C++ (except for
    some minor differences).
•   One difference between the two languages
    is that the virtual keyword is not a part of
    Java. This is because in Java all methods
    are potentially virtual, and the virtual
    keyword is therefore unnecessary.
                  Copyright 2008 -- Walter Wesley   6

•   The technique of using a pointer (VPTR) to
How to Draw Polymorphically
                                    Shape
                                  abstract
                                  void draw();




    Circle            Triangle                 Rectangle            Polygon

void draw() {...}   void draw() {...}        void draw() {...}   void draw() {...}




                           Copyright 2008 -- Walter Wesley                           7
How to Draw Polymorphically
                       Shape Objects
                        Each object will draw
                        based upon what kind of
Vector of               object it is.
References to                draw
Shape


                              draw


                              draw


                              draw


“Objects are what they are, and they do what they do.”
                                  – W. Duane Wesley

                            Copyright 2008 -- Walter Wesley   8

More Related Content

What's hot

FILTRATION-Plate and frame filter press
FILTRATION-Plate and frame filter press FILTRATION-Plate and frame filter press
FILTRATION-Plate and frame filter press sruthi's Pharma World
 
Packaging of pharmaceutical products
Packaging of pharmaceutical productsPackaging of pharmaceutical products
Packaging of pharmaceutical productsSyeda Abeer
 
Dextran presentation
Dextran presentationDextran presentation
Dextran presentationpriyanka jha
 
FBD FLUIDIZED BED DRYER
FBD       FLUIDIZED BED DRYER  FBD       FLUIDIZED BED DRYER
FBD FLUIDIZED BED DRYER AMANVYAWAHARE
 
Polymorphism and crystallisation : The mysterious phenomenon
Polymorphism and crystallisation : The mysterious phenomenonPolymorphism and crystallisation : The mysterious phenomenon
Polymorphism and crystallisation : The mysterious phenomenonMadhulika Harde
 
Immobilization of enzymes
Immobilization of enzymesImmobilization of enzymes
Immobilization of enzymeskamblesai2611
 
A lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy Gangadi
A lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy GangadiA lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy Gangadi
A lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy GangadiDr Jayapal Reddy Gangadi
 
Particle Coating (Pharmaceutics)
Particle Coating (Pharmaceutics)Particle Coating (Pharmaceutics)
Particle Coating (Pharmaceutics)Ghulam Rasool Noor
 
High pressure homogenization
High pressure homogenizationHigh pressure homogenization
High pressure homogenizationSoumya Singh
 
Mvc pattern and implementation in java fair
Mvc   pattern   and implementation   in   java fairMvc   pattern   and implementation   in   java fair
Mvc pattern and implementation in java fairTech_MX
 
IVT Presentation Batch vs Continuous - 45min_REV3
IVT Presentation Batch vs Continuous - 45min_REV3IVT Presentation Batch vs Continuous - 45min_REV3
IVT Presentation Batch vs Continuous - 45min_REV3Eric Sipe
 
15mph104 dishant scale up
15mph104 dishant scale up15mph104 dishant scale up
15mph104 dishant scale upDishant shah
 

What's hot (20)

FILTRATION-Plate and frame filter press
FILTRATION-Plate and frame filter press FILTRATION-Plate and frame filter press
FILTRATION-Plate and frame filter press
 
Packaging of pharmaceutical products
Packaging of pharmaceutical productsPackaging of pharmaceutical products
Packaging of pharmaceutical products
 
Crstallization
CrstallizationCrstallization
Crstallization
 
Lyophillization
LyophillizationLyophillization
Lyophillization
 
Dextran presentation
Dextran presentationDextran presentation
Dextran presentation
 
FBD FLUIDIZED BED DRYER
FBD       FLUIDIZED BED DRYER  FBD       FLUIDIZED BED DRYER
FBD FLUIDIZED BED DRYER
 
Polymorphism and crystallisation : The mysterious phenomenon
Polymorphism and crystallisation : The mysterious phenomenonPolymorphism and crystallisation : The mysterious phenomenon
Polymorphism and crystallisation : The mysterious phenomenon
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Immobilization of enzymes
Immobilization of enzymesImmobilization of enzymes
Immobilization of enzymes
 
A lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy Gangadi
A lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy GangadiA lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy Gangadi
A lecture on Crystallization-Pharma.Engineering-Dr Jayapal Reddy Gangadi
 
Tablet coating
Tablet coatingTablet coating
Tablet coating
 
Lec 2 polymorphism
Lec 2 polymorphismLec 2 polymorphism
Lec 2 polymorphism
 
Particle Coating (Pharmaceutics)
Particle Coating (Pharmaceutics)Particle Coating (Pharmaceutics)
Particle Coating (Pharmaceutics)
 
validation of autoclave
validation of autoclavevalidation of autoclave
validation of autoclave
 
High pressure homogenization
High pressure homogenizationHigh pressure homogenization
High pressure homogenization
 
Mvc pattern and implementation in java fair
Mvc   pattern   and implementation   in   java fairMvc   pattern   and implementation   in   java fair
Mvc pattern and implementation in java fair
 
IVT Presentation Batch vs Continuous - 45min_REV3
IVT Presentation Batch vs Continuous - 45min_REV3IVT Presentation Batch vs Continuous - 45min_REV3
IVT Presentation Batch vs Continuous - 45min_REV3
 
Enzyme immobilization
Enzyme immobilizationEnzyme immobilization
Enzyme immobilization
 
SPRAY GRANULATION
SPRAY GRANULATIONSPRAY GRANULATION
SPRAY GRANULATION
 
15mph104 dishant scale up
15mph104 dishant scale up15mph104 dishant scale up
15mph104 dishant scale up
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Polymorphism

  • 1. Polymorphism Poly → many Morph → shapes (“behaviors”) Copyright 2008 -- Walter Wesley 1
  • 2. Advanced Polymorphism • You should already have a basic understanding of polymorphism. • But do you really understand how polymorphism works? • How does the compiler actually implement polymorphism? Copyright 2008 -- Walter Wesley 2
  • 3. Polymorphism in C++ • Suppose you have a C++ class. It has a few methods, and some instance variables (fields). • In your program, you instantiate an object and you get and display the size in bytes of the object (C++ has a sizeof operator that provides the size of its operand). • You record the number of bytes. Copyright 2008 -- Walter Wesley 3 • You now change your class by placing the
  • 4. Polymorphism in C++ (continued) • Even though all you did was introduce the “virtual” keyword (in C++ the virtual keyword is used to specify that you want that method to behave polymorphically), your object has increased in size by 4 bytes. • It is as if a pointer has been added to your object (4 bytes is exactly the size of a pointer instance variable). Copyright 2008 -- Walter Wesley 4 • In fact, this is exactly what has occurred.
  • 5. Polymorphism in C++ The added points is The VPTR pointer VTABLE entries (continued) called the called to a table VPTR are pointers to (short for Virtual Pointer). the VTABLE virtual methods. MyClass VPTR VTABLE First Virtual Method Second The VTABLE entries Virtual Method are ordered in accordance with the order in which the methods are declared with the class. Copyright 2008 -- Walter Wesley 5
  • 6. Polymorphism in Java • Polymorphism in Java is implemented in exactly the same way as in C++ (except for some minor differences). • One difference between the two languages is that the virtual keyword is not a part of Java. This is because in Java all methods are potentially virtual, and the virtual keyword is therefore unnecessary. Copyright 2008 -- Walter Wesley 6 • The technique of using a pointer (VPTR) to
  • 7. How to Draw Polymorphically Shape abstract void draw(); Circle Triangle Rectangle Polygon void draw() {...} void draw() {...} void draw() {...} void draw() {...} Copyright 2008 -- Walter Wesley 7
  • 8. How to Draw Polymorphically Shape Objects Each object will draw based upon what kind of Vector of object it is. References to draw Shape draw draw draw “Objects are what they are, and they do what they do.” – W. Duane Wesley Copyright 2008 -- Walter Wesley 8