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
- A variable or
identifier is simply a name -- a handle to
access values
- A value is a string of bits -- the
current contents in computer memory
- In statically typed languages, types
associated with variables (e.g., Pascal, C)
- In dynamically typed languages, types associated with values
(e.g., Lisp, Snobol)
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:
- Instance of child class is in all important respects
representative of parent
- Thus, reasonable to assign child instance to variable of
parent class
- However, assigning parent instance to child class not generally
permitted
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
- Class of declaration is the static class for the variable
- Class of value is the dynamic class of the variable
- Even statically typed OOP languages permit these to differ
Subclasses and Subtypes
- A subclass is a class formed using inheritance
- A subtype is a class that can be substituted (using polymorphism)
with no observable effect
- Not all subclasses are subtypes
- In some languages (e.g., C++) can ONLY be formed using subclasses
- In other languages (e.g., Smalltalk, Objective-C) subclasses and
subtypes are totally independent
- In Java subtypes formed using subclasses or using interfaces
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?
- Alice holds small Mammal -- asks Bill "does this animal give
birth to live young?"
Static answer: All mammals give birth to live young, therefore yes
- What if the Mammal is a platypus?
Dynamic answer: Platypus lay eggs, therefore no
- Even statically typed OOP languages can use dynamic binding
But may use static type to determine legality of operation
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++
- Instances of classes are statically typed, not polymorphic
-- pointers and references can hold polymorphic values
- Originally, values did not have any type information
-- now does have limited info with RTTI facilities
- Method binding can optionally be performed using dynamic class for
pointers and references (for
virtual
functions)
- Legality of message passing checked at compile time based on
static type
- Originally, no facilities for the testing of reverse polymorphism
-- now does have dynamic_cast
and typeid
- Assignment of reverse polymorphic variables can be performed
using cast or
dynamic_cast
Binding in Java
Java Interfaces
- Can be implemented by classes
- Similar to classes, but have no implementation
- Support inheritance, even multiple inheritance
- Define subtypes
-- inheritance defines subclasses.
UP to CSCI 581 Lecture Notes root document?
Copyright © 1999, H. Conrad Cunningham
Last modified: Wed Apr 21 17:03:23 CDT