You are on page 1of 4

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.
Specialization
Specification
Construction
Generalization or Extension
Limitation
Variance
Specialization Inheritance
Each child class overrides a method inherited from the parent in order to specialize the class in
some way.
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).
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).
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
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.
Generally not a good idea, since it breaks the idea of substitution. But again, it is sometimes found
in practice
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
Inheritance for Combination
Combination. The child class inherits features from more than one parent class. This is
multiple inheritance and will be the subject of a later chapter.

Consider:
Subclass instances must possess all data areas of the parent
Subclass instances must implement all functionality of the parent
Thus:
Subclass instance should be indistinguishable from parent class instance -- child can be
substituted for parent
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:
Not all subclasses are subtypes
Sometimes subtypes are constructed without being subclasses
Forms of Inheritance
Specialization:
Child class is a special case (subtype) of parent
Most common form of inheritance
Example: Professor is specialized form of Employee
Child may override behavior of parent to specialize
Child satisfies specification of parent in all relevant aspects
Preserves substitutability
Specification:
Parent class defines behavior implemented in the child, but not parent
Second next most common form of inheritance
Example: class StackInArray gives implementations for method signatures defined
in abstract class Stack
Java: StackInArray extends Stack

Example: class ArrayRankedSeq gives implementations for method signatures


defined in interface RankedSequence
Java: ArrayRankedSeq implements RankedSequence

Subclasses are realizations of incomplete abstract specification


Defines common interface for group of related classes
Preserves substitutability
Interfaces and abstract classes

Construction:
Parent class used only for its behavior -- child class is not subtype -- no is-a relationship to
parent
Sometimes used for convenience, but discouraged
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 in dynamically typed languages (e.g., Smalltalk) than in statically
typed (e.g., Java)
Can sometimes use aggregation instead
Child class can modify the arguments or names of methods
Generalization:
Child class modifies or overrides some methods of parent, extends the behavior to more
general kind of object
Sometimes used for convenience (or necessity), but discouraged
Example: graphics Window generalized to ColorWindow (with background color)
Opposite of specialization -- violates substitutability
Used when must build from fixed, difficult-to-modify set of classes
Where possible, invert class hierarchy or use aggregation
Extension:
Child class adds new functionality to parent, but does not change any inherited behavior
Useful technique to give new behaviors to existing base class that cannot be
modified
Example: StringSet extends Set, adding string-related methods (e.g, prefix search)
Preserves substitutability

Limitation:
Child class limits some of the behavior of parent
Sometimes used for convenience, but strongly discouraged
Example: Stack extends DoubleEndedQueue, replacing unneeded methods to give
error messages
Violates substitutability
Used when must build from 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
Sometimes used for convenience, but discouraged
Example: graphics Tablet class extending Mouse to share similar control code
Violates substitutability
Better to define more general parent class like PointingDevice
Combination:
Child class inherits features from more than one parent -- multiple inheritance
Example: GraduateInstructor might inherit from both GraduateStudent and Faculty
Often difficult to understand and to implement language
Often use to "mix-in" specification of another role or protocol
Java has single inheritance via subclassing (extends), but multiple inheritance for
specification via interface implementations (implements)
Benefits of Inheritance
Software Reuse
Code Sharing
Improved Reliability
Consistency of Interface
Rapid Prototyping
Polymorphism
Information Hiding
Advantages of packages
o

Java package is used to categorize the classes and interfaces so that they can be
easily maintained.

Java package provides access protection.

Java package removes naming collision.

Advantages of package:
----------------------A. Modularity
- Encapsulate related constructs.
B. Easier Application Design
- Code and compile specification and body separately.
C. Hiding Information
- Only the declarations in the pacakge specification
are visible and accessible to application.
- Private constructs in the package body are hidden
and inaccesible.
- All coding is hidden in the package body.

D. Added Functionality
- Persistency of variables and coursors.
E. Better Performance
- The entire package is loaded into memory when the
package is first referenced.
- There is only one copy in memory for all users.
- The dependency hierarchy is simplified.
F. Overloading
- Multiple subprograms of the same name.