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

Drug delivery nanocarriers
Drug delivery nanocarriersDrug delivery nanocarriers
Drug delivery nanocarriersMohamed Ali
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism023henil
 
Evaluation of polymer
Evaluation of polymer Evaluation of polymer
Evaluation of polymer Divya Verma
 
Implantable polymer based drug delivery system for tailored drug release profile
Implantable polymer based drug delivery system for tailored drug release profileImplantable polymer based drug delivery system for tailored drug release profile
Implantable polymer based drug delivery system for tailored drug release profileSt. Cloud State University
 
Drug carrier delivery system
Drug carrier delivery systemDrug carrier delivery system
Drug carrier delivery systemdhara patel
 
Franz type Diffusion cell apparatus
Franz type Diffusion cell apparatusFranz type Diffusion cell apparatus
Franz type Diffusion cell apparatusanoshmilton
 
Molecular basis of targated drug delivery system
Molecular basis of targated drug delivery systemMolecular basis of targated drug delivery system
Molecular basis of targated drug delivery systemGaurav Kr
 
Characterization of nanoparticles & its regulatory aspects
Characterization of nanoparticles & its regulatory aspectsCharacterization of nanoparticles & its regulatory aspects
Characterization of nanoparticles & its regulatory aspectsvivek vyas
 
Characterization and evaluation of polymer
Characterization and evaluation of polymerCharacterization and evaluation of polymer
Characterization and evaluation of polymerChandresh Patel
 

What's hot (20)

Application and advances of polymers
Application and advances of polymersApplication and advances of polymers
Application and advances of polymers
 
Hydrogels
HydrogelsHydrogels
Hydrogels
 
Drug delivery nanocarriers
Drug delivery nanocarriersDrug delivery nanocarriers
Drug delivery nanocarriers
 
Liposomes
LiposomesLiposomes
Liposomes
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
Evaluation of polymer
Evaluation of polymer Evaluation of polymer
Evaluation of polymer
 
Implantable polymer based drug delivery system for tailored drug release profile
Implantable polymer based drug delivery system for tailored drug release profileImplantable polymer based drug delivery system for tailored drug release profile
Implantable polymer based drug delivery system for tailored drug release profile
 
Micelle presentation
Micelle presentationMicelle presentation
Micelle presentation
 
Virosomes
VirosomesVirosomes
Virosomes
 
Polymers
PolymersPolymers
Polymers
 
Drug carrier delivery system
Drug carrier delivery systemDrug carrier delivery system
Drug carrier delivery system
 
Franz type Diffusion cell apparatus
Franz type Diffusion cell apparatusFranz type Diffusion cell apparatus
Franz type Diffusion cell apparatus
 
Nanocrystals
NanocrystalsNanocrystals
Nanocrystals
 
Molecular basis of targated drug delivery system
Molecular basis of targated drug delivery systemMolecular basis of targated drug delivery system
Molecular basis of targated drug delivery system
 
Characterization of nanoparticles & its regulatory aspects
Characterization of nanoparticles & its regulatory aspectsCharacterization of nanoparticles & its regulatory aspects
Characterization of nanoparticles & its regulatory aspects
 
Characterization and evaluation of polymer
Characterization and evaluation of polymerCharacterization and evaluation of polymer
Characterization and evaluation of polymer
 
liposomes
liposomes liposomes
liposomes
 
Drug delivery systems
Drug delivery systemsDrug delivery systems
Drug delivery systems
 
Polymer Science
Polymer Science Polymer Science
Polymer Science
 
Presentation On Nanoparticles
Presentation On NanoparticlesPresentation On Nanoparticles
Presentation On Nanoparticles
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
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 ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

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