OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 12 Support for Object oriented Programming.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Overview of C++ Polymorphism
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Copyright © 2009 Elsevier Chapter 9 :: Data Abstraction and Object Orientation Programming Language Pragmatics Michael L. Scott.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
Chapter 9 :: Data Abstraction and Object Orientation
Polymorphism.
CS212: Object Oriented Analysis and Design
Types of Programming Languages
Copyright © 2009 Elsevier Chapter 9 :: Data Abstraction and Object Orientation Programming Language Pragmatics Michael L. Scott.
Polymorphism Lec
Chapter 9 :: Data Abstraction and Object Orientation
More Object-Oriented Programming
Object Oriented Programming
Chapter 9 :: Data Abstraction and Object Orientation
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Inheritance and Polymorphism
Chapter 10 :: Object Orientated Programming
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

OOP and Dynamic Method Binding Chapter 9

Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism or Subtype Polymorphism One of three key factors in OOP – Encapsulation of data and methods (data hiding) – Inheritance – Dynamic method binding

Dynamic Method Binding Ability to use a derived class in a context that expects its base class Person printLabel() Student printLabel() Professor printLabel() Student s = new Student() Professor p = new Professor() Person x = s; Person y = p; s.printLabel(); p.printLabel(); x.printLabel(); // Which one? y.printLabel();

Dynamic Method Binding If we use Person’s printLabel() then this is using static binding – We say that Student’s printLabel() redefines Person’s printLabel() If we use Student’s printLabel() this this is using dynamic binding – We say that Student’s printLabel() overrides Person’s printLabel() – This is what always happens in Java C++ and C# let you do both x.printLabel();

Virtual Methods Methods that can be overridden are called virtual methods – You might never have seen this term before since it’s not used in Java because all methods are virtual In C++: class person { public: virtual void printLabel(); Normally virtual methods are used when the object doesn’t know what implementation is to be used at compile time

Abstract Classes In most OOP languages we can omit the body of a virtual method in a base class Java and C# use the keyword abstract – A class defined as abstract must have at least one abstract method on it C++ uses assignment to 0 abstract class person { public abstract void printLabel(); class person { public: virtual void printLabel() = 0; An interface is identical to an abstract class with all abstract methods

Dynamic Method Binding Non-virtual methods require no space at run time; the compiler just calls the appropriate version, based on type of variable – Member functions are passed an extra, hidden, initial parameter: this (called Me in VB and self in Smalltalk) C++ philosophy is to avoid run-time overhead whenever possible(Sort of the legacy from C) – Languages like Smalltalk have (much) more run- time support

Dynamic Method Binding Virtual functions are the only thing that requires any trickiness – They are implemented by creating a dispatch table (vtable) for the class and putting a pointer to that table in the data of the object – Objects of a derived class have a different dispatch table In the dispatch table, functions defined in the parent come first, though some of the pointers point to overridden versions

Implementation of Virtual Methods vtable = virtual method table

Implementation of Virtual Methods

Foo *f = new Foo(); Bar *b = new Bar(); Foo *q = b; Bar *s = f;// static semantic error Foo f; Bar b; Foo q = b; Bar s = f; // Error Different on Stack:

Dynamic Type Binding Note that if you can query the type of an object, then you need to be able to get from the object to run-time type info – The standard implementation technique is to put a pointer to the type info at the beginning of the vtable – Of course you only have a vtable in C++ if your class has virtual functions