Introduction to Object Oriented Programming, 3rd Ed

Timothy A. Budd

Chapter 8

Inheritance and Substitution

Outline

  1. Roadmap
  2. Abstract idea of Inheritance
  3. Practical Meaning of Inheritance
  4. Private Public and Protected
  5. Inheritance is both Extension and Contraction
  6. The Is-a rule
  7. Reuse of Code, Reuse of Concept
  8. Syntax for Inheritance
  9. Trees vs Forests
    1. A Portion of the Little Smalltalk Hierarchy
  10. An Argument for Substitution
  11. Subclass vs Subtype
  12. Syntax for Overriding
  13. Interfaces and Abstract Classes
  14. Forms of Inheritance
    1. Specialization Inheritance
    2. Specification Inheritance
    3. Inheritance for Construction
    4. Inheritance for Generalization or Extension
    5. Inheritance for Limitation
    6. Inheritance for Variance
    7. Summary of Forms of Inheritance
  15. Benefits of Inheritance
  16. The Costs of Inheritance
  17. Chapter Summary

Other Material

Intro OOP, Chapter 7, outline

Roadmap

In this chapter we will start to investigate the concepts of inheritance and substitution.
Intro OOP, Chapter 8, Slide 01

Abstract idea of Inheritance

We motivated the idea of inheritance with a hierarchy of categories:

hierarchy of categories

Intro OOP, Chapter 8, Slide 02

Practical Meaning of Inheritance

Note that private aspects of the parent are part of the child, but are not accessible within the child class.

Intro OOP, Chapter 8, Slide 03

Private, Public and Protected

There are now three levels of visibility modifiers: Note: Java interprets protected to mean accessible within same package
Intro OOP, Chapter 8, Slide 04

Inheritance is both Extension and Contraction

This interplay between inheritance and overriding, extension and contraction, is what allows object-oriented systems to take very general tools and specialize them for specific projects. This interplay is ultimately the source of a great deal of the power of OOP.
Intro OOP, Chapter 8, Slide 05

The is-a Rule

Our idealization of inheritance is captured in a simple rule-of-thumb.

Try forming the English sentences ``An A is-a B''. If it ``sounds right'' to your ear, then A can be made a subclass of B.

A dog is-a mammal, and therefore a dog inherits from mammal

A car is-a engine sounds wrong, and therefore inheritance is not natual. but a car has-a engine.

Intro OOP, Chapter 8, Slide 06

Reuse of Code, Reuse of Concept

Why do we use inheritance? Basically there are two major motivations: An example of the latter from the case study in chapter 7, all graphical objects know how to draw.
Intro OOP, Chapter 8, Slide 07

Syntax for Inheritance

Languages use a variety of different syntax to indicate inheritance:
class Wall : public GraphicalObject  -- c++

class Wall extends GraphicalObject -- Java

class Wall : GraphicalObject -- C#

(defclass Wall (GraphicalObject) () ) -- CLOS

type Wall = object (GraphicalObject) -- Object Pascal

class Wall < GraphicalObject -- Ruby
Intro OOP, Chapter 8, Slide 08

Trees vs Forests

There are two common views of class hierarchies:

Intro OOP, Chapter 8, Slide 09

A portion of the Little Smalltalk Hierarchy

smalltalk inheritance hierarchy

Intro OOP, Chapter 8, Slide 10

An Argument for Substitution

Consider the following argument:

Intro OOP, Chapter 8, Slide 11
The principle of substitutability is sometimes called Liskov substitutability, since one of the first people to describe the idea was Barbara Liskov, of MIT.

Subclass vs Subtype

Of course, the problem with this argument is that a child class can override a method and make arbitrary changes. It is therefore useful to define two separate concepts: It is possible to form subclasses that are not subtypes; and (in some languages at least) form subtypes that are not subclasses.
Intro OOP, Chapter 8, Slide 12

(We will have much more to say on this topic in a later chapter).

Syntax for Overriding

Some languages, such as C++, require that the programmer indicate in the parent class that overriding is a potential:

	class GraphicalObject {
	public:
		virtual void draw();  // can be overridden
	};

Other languages, such as Object Pascal, require a modifier in the child class that overriding has taken place:
type
	Ball = object (GraphicalObject)
		...
		procedure draw; override; (* overriding has taken place *)
	end
Still other languages (C#, Delphi) require indications in both parent and child.
And some languages (Smalltalk) do not require any indication in either parent class or child class.
Intro OOP, Chapter 8, Slide 13

Interfaces and Abstract Classes

An interface is similar to a class, but does not provide any implementation. A child class must override all methods. A middle ground is an abstract class. Here some methods are defined, and some (abstract methods) are undefined. A child class must fill in the definition for abstract methods:
abstract class Window {
	...
	abstract public void paint (); // child class must redefine
	...
}
An interface is like an abstract class in which all methods are abstract. In C++ an abstract method is called a pure virtual method.
Intro OOP, Chapter 8, Slide 14

Forms of Inheritance

The choices between inheritance and overriding, subclass and subtypes, mean that inheritance can be used in a variety of different ways and for different purposes. Many of these types of inheritance are given their own special names. We will describe some of these specialized forms of inheritance.
Intro OOP, Chapter 8, Slide 15

Specialization Inheritance

By far the most common form of inheritance is for specialization.

A good example is the Java hierarchy of Graphical components in the AWT:

Each child class overrides a method inherited from the parent in order to specialize the class in some way.
Intro OOP, Chapter 8, Slide 16

Specification Inheritance

If the parent class is abstract, we often say that it is providing a specification for the child class, and therefore it is specification inheritance (a variety of specialization inheritance).

Example: Java Event Listeners

ActionListener, MouseListener, and so on specify behavior, but must be subclassed.

Intro OOP, Chapter 8, Slide 17

Inheritance for Construction

If the parent class is used as a source for behavior, but the child class has no is-a relationship to the parent, then we say the child class is using inheritance for construction.

An example might be subclassing the idea of a Set from an existing List class.

Generally not a good idea, since it can break the principle of substituability, but nevertheless sometimes found in practice. (More often in dynamically typed languages, such as Smalltalk).

Intro OOP, Chapter 8, Slide 18

Inheritance for Generalization or Extension

If a child class generalizes or extends the parent class by providing more functionality, but does not override any method, we call it inheritance for generalization.

The child class doesn't change anything inherited from the parent, it simply adds new features.

An example is Java Properties inheriting form Hashtable.

Intro OOP, Chapter 8, Slide 19

Inheritance for Limitation

If a child class overrides a method inherited from the parent in a way that makes it unusable (for example, issues an error message), then we call it inheritance for limitation.

For example, you have an existing List data type that allows items to be inserted at either end, and you override methods allowing insertion at one end in order to create a Stack.

Generally not a good idea, since it breaks the idea of substitution. But again, it is sometimes found in practice.

Intro OOP, Chapter 8, Slide 20

Inheritance for Variance

Two or more classes that seem to be related, but its not clear who should be the parent and who should be the child.

Example: Mouse and TouchPad and JoyStick

Better solution, abstract out common parts to new parent class, and use subclassing for specialization.

Intro OOP, Chapter 8, Slide 21

Summary of Forms of Inheritance

Intro OOP, Chapter 8, Slide 22

Benefits of Inheritance

Intro OOP, Chapter 8, Slide 23

Cost of Inheritance

This does not mean you should not use inheritance, but rather than you must understand the benefits, and weigh the benefits against the costs.

Intro OOP, Chapter 8, Slide 24

Chapter Summary

In this chapter we have begun the exploration of inheritance, a topic we will continue through the next several chapters. Topics we have addressed have included the following:
Intro OOP, Chapter 8, Slide 25