Introduction to Object Oriented Programming, 3rd Ed

Timothy A. Budd

Chapter 10

Subclasses and Subtypes

Outline

  1. Roadmap
  2. Subtype, Subclass and Substitution
  3. What is a Type?
    1. The Problem of Defining Types
  4. The Definition of Subtype
  5. Subclasses are not Necessarily Subtypes
  6. The Substitution Paradox
    1. The Undecidability of the Subtype Relationship
    2. Is This a Problem?
  7. Chapter Summary

Other Material

Intro OOP, Chapter 10, Outline

Roadmap

In this chapter we will explore the relationships between the two concepts of subclass and subtype.
Intro OOP, Chapter 10, Slide 01

Subtype, Subclass and Substitution

The distinction between subtype and subclass is important because of their relationship to substitution.

Recall the argument that asserted a child class has the same behavior as the parent, and thus a variable declared as the parent class should in fact be allowed to hold a value generated from a child class.

But does this argument always hold true?

Intro OOP, Chapter 10, Slide 02

What is a type?

What do we mean when we use the term type in describing a programming language? What about when we consider classes (or interfaces) as a system for defining types?
Intro OOP, Chapter 10, Slide 03

The Problem of Defining Types

Consider how we might define a Stack ADT:
interface Stack {
	public void push (Object value);
	public Object top ();
	public void pop ();
}
Notice how the interface itself says nothing about the LIFO property, which is the key defining feature of a stack. Is the following a stack?
class NonStack implements Stack {
	public void push (Object value) { v = value; }
	public Object top () { return v; }
	public void pop () { v = null; }

	private Object v = null;
}
Intro OOP, Chapter 10, Slide 04

The Definition of Subtype

So now we can better understand the concept of a subtype.

A subtype preserves the meaning (purpose, or intent) of the parent.

Problem, meaning is extremely difficult to define. Think about how to define the LIFO characteristics of the stack.

Intro OOP, Chapter 10, Slide 05

Subclasses are not Necessarily Subtypes

It is easy to create a subclass that is not a subtype -- think of NonStack.

It is also possible to create subtypes that are not subclasses. Think of Array in Smalltalk. This class is characterized by the following interface:

	at: input put: value
	at: int
	size
But class Dictionary will also support the same interface, and perform similar actions -- but Dictionary is not a subclass of Array.
Intro OOP, Chapter 10, Slide 06

The Substitution Paradox

There is a curious paradox that lies at the heart of most strongly typed object-oriented programming languages. If substitution only makes sense for subtypes and not for all subclasses, why do programming languages based the validity of assignment on subclasses?
Intro OOP, Chapter 10, Slide 07

The Undecidability of the Subtype Relationship

It is trivial to determine if one class is a subclass of another.

It is extremely difficult to define meaning (think of the Stack ADT), and even if you can it is almost always impossible to determine if one class preserves the meaning of another.

One of the classic corollaries of the halting problem is that there is no procedure that can determine, in general, if two programs have equivalent behavior.

Intro OOP, Chapter 10, Slide 08

Is This a Problem?

What does it take to create a subclass that is not a subtype? Is this common? Not likely. But it shows you where to look for problem areas.
Intro OOP, Chapter 10, Slide 09

Chapter Summary

Intro OOP, Chapter 10, Slide 10