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

liquid crystal phase
liquid crystal phaseliquid crystal phase
liquid crystal phase
Raees Ahmad
 
The cell membrane
The cell membraneThe cell membrane
The cell membrane
IIDC
 

What's hot (20)

Anomers and epimers
Anomers and epimersAnomers and epimers
Anomers and epimers
 
Plasma membrane
Plasma membranePlasma membrane
Plasma membrane
 
Fungi
Fungi Fungi
Fungi
 
DILATANT FLUIDS.pptx
DILATANT FLUIDS.pptxDILATANT FLUIDS.pptx
DILATANT FLUIDS.pptx
 
liquid crystal phase
liquid crystal phaseliquid crystal phase
liquid crystal phase
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Endoplasmic reticulum
Endoplasmic reticulum Endoplasmic reticulum
Endoplasmic reticulum
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Difference between prokaryotes and eukaryotes
Difference between prokaryotes and eukaryotesDifference between prokaryotes and eukaryotes
Difference between prokaryotes and eukaryotes
 
Exocytosis, endocytosis
Exocytosis, endocytosisExocytosis, endocytosis
Exocytosis, endocytosis
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Cell membrane
Cell membraneCell membrane
Cell membrane
 
Golgi body
Golgi bodyGolgi body
Golgi body
 
The cell membrane
The cell membraneThe cell membrane
The cell membrane
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 
Plasma membrane
Plasma membranePlasma membrane
Plasma membrane
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Covalent and non Covalent interaction in Macromolecules
Covalent and non Covalent interaction in MacromoleculesCovalent and non Covalent interaction in Macromolecules
Covalent and non Covalent interaction in Macromolecules
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Enzymes
EnzymesEnzymes
Enzymes
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
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
QucHHunhnh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

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