CSci 581: Obj.-Oriented Design & Programming
Spring Semester 1999
Lecture Notes


Subclasses and Subtypes

This Web document was based on a set of "slides" created by the instructor for use in the Fall 1997 offering of the object-oriented design and programming course. That set was, in turn, based on a set of slides created by Timothy Budd to supplement chapter 10 of his textbook An Introduction to Object-Oriented Programming, Second Edition (Addison-Wesley, 1997).


Related Concepts

The following concepts are related, but are not exactly the same:

Static versus Dynamic Typing
how variables and values are related

Static versus Dynamic Classes
values a variable can maintain

Static versus Dynamic Method Binding
how method invocation is understood

Subclass versus Subtype
how a class can be used in place of another


Typing


Error Detection with Static Typing

Statically typed languages permit type error detection at compile time

    double r;

    r= 'x';

Above results in a compiler error message


Polymorphic Variables

Object-oriented concepts introduce a new twist:

    class Dog extends Mammal { ... }
    class Cat extends Mammal { ... }
    ...
    Mammal pet;
    Dog fido;
    Cat felice;
    pet  = fido;    // legal
    pet  = felice;  // legal
    fido = pet;     // illegal !


Static and Dynamic Classes

    class Dog extends Mammal { ... }
    class Cat extends Mammal { ... }
    ...
    Mammal pet;
    Dog fido;
    Cat felice;
    pet = fido;	// legal


Subclasses and Subtypes


Reverse Polymorphism

Polymorphism allows value from child class to be assigned to variable of parent class

Under what conditions can value of parent class be assigned to variable of child class?

    class Dog extends Mammal { ... }
    class Cat extends Mammal { ... }
    ...
    Mammal pet;
    Dog fido;
    Cat felice;
    pet = fido; // legal
    fido = pet; // is this legal? 

This is the problem of reverse polymorphism


Two Aspects of Reverse Polymorphism

Problem of identity:
whether a variable declared as parent class actually holds value from subclass

Task of assignment:
assigning a value from parent class to a variable declared as a subclass

Some languages mechanisms address these two problems together

Other languages separate them


The Container Problem


Static and Dynamic Method Binding

Should binding be on static class of variable or dynamic class of value?


Merits of Static versus Dynamic Binding

Efficiency:
static binding requires fewer CPU cycles

Error detection:
static binding permits error detection at compile time

Flexibility:
dynamic binding permits greater flexibility, static binding creates rigidity and inhibits reuse


Binding in Smalltalk


Binding in C++


Binding in Java


Java Interfaces


UP to CSCI 581 Lecture Notes root document?


Copyright © 1999, H. Conrad Cunningham
Last modified: Wed Apr 21 17:03:23 CDT