1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
1 Object-Oriented Programming (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
1 Data Abstraction and Object Orientation. 2 Chapter 9: Data Abstraction and Object Orientation 9.1 Encapsulation and Inheritance 9.2 Initialization and.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 23: Object Lifetime and Garbage Collection COMP 144 Programming Language Concepts.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 22: Object-Oriented Programming COMP 144 Programming Language Concepts Spring 2002.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Objected Oriented Programming & Design JAVA Shishir Gupta (704) (704)
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.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Support for Object oriented Programming.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ISBN Object-Oriented Programming Chapter Chapter
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Design issues for Object-Oriented Languages
Polymorphism in Methods
Modern Programming Tools And Techniques-I
OOP What is problem? Solution? OOP
Inheritance and Run time Polymorphism
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Types of Programming Languages
Object Oriented Analysis and Design
More Object-Oriented Programming
Polymorphism Polymorphism
Parameter Passing Actual vs formal parameters
Support for Object-Oriented Programming
Inheritance and Polymorphism
Lecture 10 Concepts of Programming Languages
Programming in C# CHAPTER 5 & 6
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos March 20 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Fundamental Concepts in OOP EncapsulationEncapsulation –Data Abstraction –Information hiding –The notion of class and object InheritanceInheritance –Code reusability –Is-a vs. has-a relationships PolymorphismPolymorphism –Dynamic method binding

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Fundamental Concepts in OOP EncapsulationEncapsulation –Data Abstraction –Information hiding –The notion of class and object InheritanceInheritance –Code reusability –Is-a vs. has-a relationships PolymorphismPolymorphism –Dynamic method binding

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Inheritance Encapsulation improves code reusabilityEncapsulation improves code reusability –Abstract Data Types –Modules –Classes However, it is generally the case that the code a programmer wants to reuse is close but not exactly what the programmer needsHowever, it is generally the case that the code a programmer wants to reuse is close but not exactly what the programmer needs Inheritance provides a mechanism to extend or refine units of encapsulationInheritance provides a mechanism to extend or refine units of encapsulation –By adding or overriding methods –By adding attributes

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Inheritance Notation Java.awt.Dialog Java.awt.FileDialog Base Class (or Parent Class or Superclass) Derived Class (or Child Class or Subclass) Is-a relationship

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Inheritance Subtype Java.awt.Dialog Java.awt.FileDialog Base Class Derived Class Is-a relationship The derived class has all the members (i.e. attributes and methods) of the base classThe derived class has all the members (i.e. attributes and methods) of the base class –Any object of the derived class can be used in any context that expect an object of the base class –fp = new FileDialog() is both an object of class Dialog and an object of class File Dialog

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Method Binding Classes Student and Professor derive from class Person Method print_mailing_list is polymorphic Results depend on the binding: static or dynamic Print_mailing_label redefined for student and professor classes

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Method Binding Static and Dynamic In static method binding, method selection depends on the type of the variable x and yIn static method binding, method selection depends on the type of the variable x and y –Method print_mailing_label() of class person is executed in both cases –Resolved at compile time In dynamic method binding, method selection depends on the class of the objects s and pIn dynamic method binding, method selection depends on the class of the objects s and p –Method print_mailing_label() of class student is executed in the first case, while the corresponding methods for class professor is executed in the second case –Resolved at run time

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Polymorphism and Dynamic Binding The is-a relationship supports the development of generic operations that can be applied to objects of a class and all its subclassesThe is-a relationship supports the development of generic operations that can be applied to objects of a class and all its subclasses –This feature is known as polymorphism –E.g. paint() method is polymorphic (accepts multiple types) The binding of messages to method definitions is instance-dependent, and it is known as dynamic bindingThe binding of messages to method definitions is instance-dependent, and it is known as dynamic binding –It has to be resolved at run-time –Dynamic binding requires the virtual keyword in C++ –Static binding requires the final keyword in Java

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Binding Implementation A common implementation is based on a virtual method table (vtable)A common implementation is based on a virtual method table (vtable) –Each object keeps a pointer to the vtable that corresponds to its class

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Binding Implementation Given an object of class foo, and pointer f to this object, the code that is used to invoked the appropriate method would beGiven an object of class foo, and pointer f to this object, the code that is used to invoked the appropriate method would be this (self) (polymorphic) method invocation

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Binding Implementation Simple Inheritance Derived classes extend the vtable of their base classDerived classes extend the vtable of their base class –Entries of overridden methods contain the address of the new methods

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Binding Implementation Multiple Inheritance A class may derive from more that one base classA class may derive from more that one base class –This is known as multiple inheritance Multiple inheritance is also implemented using vtablesMultiple inheritance is also implemented using vtables –Two cases »Non-repeated multiple inheritance »Repeated multiple inheritance

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Method Binding Non-Repeated Multiple Inheritance

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Method Binding Non-Repeated Multiple Inheritance The view of this must be corrected, so it points to the correct part of the objectsThe view of this must be corrected, so it points to the correct part of the objects –An offset d is use to locate the appropriate vtable pointer »d is known at compile time this(self)

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Dynamic Method Binding Repeated Multiple Inheritance Multiple inheritance introduces a semantic problem: method name collisionsMultiple inheritance introduces a semantic problem: method name collisions –Ambiguous method names –Some languages support inherited method renaming (e.g. Eiffel) –Other languages, like C++, require a reimplementation that solves the ambiguity –Java solves the problem by not supporting multiple inheritance »A class may inherit multiple interfaces, but, in the absence of implementations, the collision is irrelevant

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment ScottScott –Read Sect –Read Sect intro and