Idealized Image of inheritance
Subtype: class that satisfies substitutability
Subclass: Something constructed using inheritance, weather or not satisfies the Principle of substitutability.
The two concepts are independent:
Forms of inheritance
Most common form of inheritance
Example: Professor is
a specialize form of Employee
preserves substitutability
Specification: Parent class defines behavior implemented in the child, but not parent
Second most common form of inheritance
Example: abstract class Stack
defines methods for class StackInArray
preserves substitutability
Subclasses are realizations of incomplete abstract specification
Defines common interface for group of related classes
Constructor: Parent class used only for its behavior - child class is not subtype - no is-a relationship to parent
Example: extending List class to develop
Set, without "hiding" unneeded methods
Example: extending a byte-based I/O stream to
a stream for handling other objects Often violates substitutability
More common i dynamically typed languages than in statically typed
Can sometimes use aggregation instead
Generalization: Child class modifies or overrides some methods of parent, extends the behavior to more general kind of object
Example: graphical Window generalized to
Color Window (with back ground Color)
Opposite of specialization - violates substitutability
Useful when must build from fixed, difficult-to-modify set of classes Where possible, better to invert class hierarchy or se aggregation
Extension: Child class adds new functionality to parent , but does not change any inherited behavior
Example: String Set extends Set , adding string- related methods (e.g., prefix search) Preserves substitutability
Limitation: child class limits some of the behavior of parent
Example: Stack extends doubleEndedQueue, replacing unneeded methods to give error messages
Violates substitutability
Useful when must build fro fixed, difficult-to modify set of classes
Avoid when possible ,perhaps use aggregation
Variance: Child and parent class are variants of each other- inheritance to allow code sharing- arbitrary relationship
Example: graphics tablet class extending Mouse
to share similar control code violates substitutability
Better to define more general parent class like Pointing
Device
Combination: Child class inherits features from more than one parent - multiple inheritance
Example: graduate instructor inherits from both Graduate student and Faculty
Often difficult to implement and understand
Often use of "mix-in" specification o another role
or protocol
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
Disadvantages: tight coupling
of classes, large libraries for an application
Languages: Smalltalk,Objective
-C Delphi object Pascal, Java
Forest: classes only placed in hierarchies if they have a relationship - many small hierarchies.
Advantages: smaller libraries
of classes for application, less coupling possible
Disadvantage: no shared functionality
among all objects
Languages: C++, Apple Object
Pascal
// data definitions plus signatures and
// possibly implements of methods
}
public class stackInArray extends stack {
// extended features plus overridden implementation
}
Public interface Queue {
// signatures of public methods Queues must provide
}
Public class QueueAsLinkedList implements Queue {
// includes implementations of the queue methods
}