SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Destaque (13)

Современные инъекционные методы в косметологии
Современные инъекционные методы в косметологииСовременные инъекционные методы в косметологии
Современные инъекционные методы в косметологии
 
Cartaz Alunos e-IPP
Cartaz Alunos e-IPPCartaz Alunos e-IPP
Cartaz Alunos e-IPP
 
StoryBoards
StoryBoardsStoryBoards
StoryBoards
 
Flipped Classroom
Flipped ClassroomFlipped Classroom
Flipped Classroom
 
Mapa conceitual 1
Mapa conceitual 1Mapa conceitual 1
Mapa conceitual 1
 
15 Reasons Why Working at GumGum Rocks
15 Reasons Why Working at GumGum Rocks15 Reasons Why Working at GumGum Rocks
15 Reasons Why Working at GumGum Rocks
 
IMPROVING FINANCIAL LITERACY IN EUROPE: CONTRIBUTIONS OF THE EFINLIT PROJECT
IMPROVING FINANCIAL LITERACY IN EUROPE: CONTRIBUTIONS OF THE EFINLIT PROJECTIMPROVING FINANCIAL LITERACY IN EUROPE: CONTRIBUTIONS OF THE EFINLIT PROJECT
IMPROVING FINANCIAL LITERACY IN EUROPE: CONTRIBUTIONS OF THE EFINLIT PROJECT
 
Mobile Learning para transformar a educação e envolver os alunos
Mobile Learning para transformar a educação e envolver os alunos Mobile Learning para transformar a educação e envolver os alunos
Mobile Learning para transformar a educação e envolver os alunos
 
Beyond Agile: Self-Organisation for Tech Teams
Beyond Agile: Self-Organisation for Tech TeamsBeyond Agile: Self-Organisation for Tech Teams
Beyond Agile: Self-Organisation for Tech Teams
 
20161216 guiacncercolorrectal
20161216 guiacncercolorrectal20161216 guiacncercolorrectal
20161216 guiacncercolorrectal
 
Cancer colo rectal
Cancer colo rectalCancer colo rectal
Cancer colo rectal
 
Cornell Computational Chemistry Seminar
Cornell Computational Chemistry SeminarCornell Computational Chemistry Seminar
Cornell Computational Chemistry Seminar
 
a pattern for PWA, PRPL
a pattern for PWA, PRPLa pattern for PWA, PRPL
a pattern for PWA, PRPL
 

Último

4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Último (20)

4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.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