Dynamic Binding (Section 10.4) - PowerPoint PPT Presentation

About This Presentation
Title:

Dynamic Binding (Section 10.4)

Description:

f; -- reverse assignment, with dynamic check ... static lookup invalid memory access ... how do we pass such a function f (with ANY number of parameters) ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 25
Provided by: csU50
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Binding (Section 10.4)


1
Dynamic Binding(Section 10.4)
  • CSCI 431 Programming Languages
  • Fall 2003

A compilation of material developed by Felix
Hernandez-Campos and Mircea Nicolescu
2
Fundamental Concepts in OOP
  • Encapsulation
  • Data Abstraction
  • Information hiding
  • The notion of class and object
  • Inheritance
  • Code reusability
  • Is-a vs. has-a relationships
  • Polymorphism
  • Dynamic method binding

3
Inheritance
  • Encapsulation 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 needs
  • Inheritance provides a mechanism to extend or
    refine units of encapsulation
  • By adding or overriding methods
  • By adding attributes

4
InheritanceNotation
Base Class (or Parent Class or Superclass)
Java.awt.Dialog
Is-a relationship
Derived Class (or Child Class or Subclass)
Java.awt.FileDialog
5
Dynamic Method Binding
  • Consequence of inheritance
  • derived class D has all members of its base class
    B
  • can use an object of class D everywhere an object
    of class B is expected
  • a form of polymorphism

// Both student and professor objects have all
properties of // a person object // Both can be
used in a person context
6
Dynamic Method Binding
7
Dynamic Method Binding
8
Dynamic Method Binding
  • Two alternatives for choosing the method to call
  • according to the types of variables (references)
    x and y static method binding (will call the
    method of person in both cases)
  • according to the types of objects s and p to
    which x and y refer dynamic method binding
    (will call the methods of student / professor)

9
Dynamic Method Binding
  • Simula, C, Ada 95
  • static method binding by default
  • how do we specify dynamic binding in C?
  • label individual methods as virtual

10
Virtual and Non-Virtual Methods
11
Virtual and Non-Virtual Methods
12
Virtual and Non-Virtual Methods
13
Abstract Classes
14
Member Lookup
15
Member Lookup
  • The compiler will generate

16
Member Lookup
17
Member Lookup
ok references through q will use prefixes of B's
data space and vtable
static semantic error F lacks the additional
data and vtable entries of a bar
18
Type Checking
still an error
ok, but risky programmer must ensure that q
refers to a bar object
C specific involves dynamic type check
19
Type Checking
20
Type Checking Implementation
21
Member Lookup
  • Disadvantages of virtual methods
  • run-time cost
  • preclude in-line expansion of methods at compile
    time
  • performance decrease for small and frequently
    called methods
  • Potential problem with static lookup
  • fragile base class problem (Java)
  • large standard library, evolving over time
    (adding new features)
  • user may have old library and run code designed
    for new library
  • code may use the new features
  • static lookup invalid memory access
  • dynamic lookup better, produce error message
    ("member not found")

22
Classes as "Closures"
23
Classes as "Closures"
24
Classes as "Closures"
Write a Comment
User Comments (0)
About PowerShow.com