Engr 691-10: Special Topics in Engineering Science
Software Architecture
Spring Semester 2002
Lecture Notes


Understanding Inheritance (Budd's UOOPJ, Ch. 8)

This is a set of "slides" to accompany chapter 8 of Timothy Budd's textbook Understanding Object-Oriented Programming with Java, Updated Edition (Addison-Wesley, 2002).


Motivation for Inheritance

Use inheritance to create new software structures from existing software units to:


Generality and Specialization in Software Development

Conflict:

Resolution?


Abstract Idea of Inheritance

The abstract idea of inheritance is the grouping of "objects" into a hierarchy of categories.

Material_Object
        Non-Living_Thing
                Rock
                Air
        Living_Thing
                Plant
                Animal
                        Reptile
                        Mammal
                                Human_Being
                                        Dentist
                                                Roy
                                        Shopkeeper
                                                Flo
                                        Writer
                                                John
                                Cat
                                Dog
                                Platypus


Practical Meaning of Inheritance

In programming languages, inheritance means:

With respect to the parent class, a child class is, in some sense:


Idealized Image of Inheritance

Consider:

Thus:

Principle of Substitutability:
If C is a subclass of P, instances of C can be substituted for instances of P in any situation with no observable effect.


Subclass, Subtype, and Substitutability

Subtype:
class that satisfies principle of substitutability

Subclass:
something constructed using inheritance, whether or not it satisfies the principle of substitutability.

The two concepts are independent:


Forms of Inheritance

Specialization:
Child class is a special case (subtype) of parent

Specification:
Parent class defines behavior implemented in the child, but not parent

Construction:
Parent class used only for its behavior -- child class is not subtype -- no is-a relationship to parent

Generalization:
Child class modifies or overrides some methods of parent, extends the behavior to more general kind of object

Extension:
Child class adds new functionality to parent, but does not change any inherited behavior

Limitation:
Child class limits some of the behavior of parent

Variance:
Child and parent class are variants of each other -- inheritance to allow code sharing -- arbitrary relationship

Combination:
Child class inherits features from more than one parent -- multiple inheritance


Inheritance and Assertions

Suppose C is a subtype of P:

Subtype C should not violate I, Q, and R:

Abstract preconditions can enable controlled "strengthening" of precondition


Trees versus Forests

There are two common views of class hierarchies.

Tree:
all classes part of single class hierarchy

Advantage:
root's functionality inherited by all objects -- all have basic functionality

Disadvantage:
tight coupling of classes, large libraries for an application

Languages: Java's classes, Smalltalk, Objective C, Delphi Object Pascal

Forest:
classes only placed in hierarchies if they have a relationship -- many small hierarchies.

Advantage:
smaller libraries of classes for application, less coupling possible

Disadvantage:
no shared functionality among all objects

Languages: Java's interfaces, C++, Apple Object Pascal


Inheritance in Java

Tree-structured class hierarchy

Forest-structured interface hierarchy

Modifiers for classes/interfaces

Visibility modifiers for class/interface features


Inheritance in Java

public abstract class Stack 
{       // extends Object by default
	// data definitions plus signatures and
	//     possibly implementations of methods 
}

public class StackInArray extends Stack 
{	// extended features plus overridden implementations
}

public interface Queue 
{	// signatures of public methods Queues must provide
}

public class QueueAsLinkedList implements Queue 
{	// includes implementations of the Queue methods
}


Facilities of Root Class Object

Minimum functionality for all objects include:

equals(Object obj)
is obj the same as receiver?

toString()
converts the object to a string value

hashCode()
return a default hashcode for the object

getClass()
return an identifier for the class of the object

The first three above are often overridden in classes.


Benefits of Inheritance


Costs of Inheritance


UP to ENGR 691 Lecture Notes root document?


Copyright © 2002, H. Conrad Cunningham
Last modified: Sun 12 Oct 2003