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

DOC
NOTULEN35
PPT
Prevencion de cancer
PDF
Exercices1erebacphy chimie
DOCX
The Escape
PDF
Oculus Rift YouTube Content
PDF
The Advertising Collective Company presentation 2016
PPTX
Case Study: Managing a Metrics Initiative
PPTX
Desafios para os Docentes do Ensino Superior na Era Digital
NOTULEN35
Prevencion de cancer
Exercices1erebacphy chimie
The Escape
Oculus Rift YouTube Content
The Advertising Collective Company presentation 2016
Case Study: Managing a Metrics Initiative
Desafios para os Docentes do Ensino Superior na Era Digital

Viewers also liked (13)

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

Similar to Polymorphism (20)

PPTX
pointers,virtual functions and polymorphism
PDF
Polymorphism
PPT
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
PPT
Polymorphism in C++ for beginners reference
PPT
Polymorphism.pptthis is oops one of the most important feature polymorphism
PPT
this is the concept in C++ under object oriented programming language "POLYMO...
PPTX
C++ Object Oriented Programming
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
PPTX
Polymorphism
PPTX
Pointers,virtual functions and polymorphism cpp
PDF
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
PPT
Lecture6.ppt
PPTX
pointer, virtual function and polymorphism
PPT
Virtual Function
PPT
C96e1 session3 c++
PPTX
OOPS & C++(UNIT 4)
PPTX
2CPP10 - Polymorphism
PPTX
Pointers, virtual function and polymorphism
PDF
Chapter27 polymorphism-virtual-function-abstract-class
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
pointers,virtual functions and polymorphism
Polymorphism
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
Polymorphism in C++ for beginners reference
Polymorphism.pptthis is oops one of the most important feature polymorphism
this is the concept in C++ under object oriented programming language "POLYMO...
C++ Object Oriented Programming
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Polymorphism
Pointers,virtual functions and polymorphism cpp
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Lecture6.ppt
pointer, virtual function and polymorphism
Virtual Function
C96e1 session3 c++
OOPS & C++(UNIT 4)
2CPP10 - Polymorphism
Pointers, virtual function and polymorphism
Chapter27 polymorphism-virtual-function-abstract-class
Object Oriented Programming (OOP) using C++ - Lecture 4
Ad

Recently uploaded (20)

PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
HVAC Specification 2024 according to central public works department
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
advance database management system book.pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
HVAC Specification 2024 according to central public works department
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Introduction to pro and eukaryotes and differences.pptx
Race Reva University – Shaping Future Leaders in Artificial Intelligence
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
A powerpoint presentation on the Revised K-10 Science Shaping Paper
advance database management system book.pdf
What’s under the hood: Parsing standardized learning content for AI
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
AI-driven educational solutions for real-life interventions in the Philippine...

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