Generalization

Another special relationship is the relationship between a subclass and a superclass. This relationship is shown with the generalization arrow. In the following diagram we say that A generalizes B which generalizes C and D:

 demo2

Java terminology is better. We say that C and D extend B which extends A.

The transitive closure of the generalization relationship is the subclass relationship. X is a subclass of Y, written X <: Y, if X is connected to Y by a chain of zero or more generalization arrows. It's easy to see the subclass relationship is transitive and reflexive:

X <: X
X <: Y <: Z => X <: Z

Semantically, X is a subclass of Y means that instances of X can be substituted for instances of Y in most contexts.

This means that any operation an instance of Y can perform can also be performed by an instance of X (although perhaps using a different algorithm.) This is another way of saying that a subclass inherits the attributes and operations of all of its superclasses.

Here's an example of an inheritance hierarchy:

Note that Shape has six subclasses:

Circle <: Ellipse <: Shape, hence Circle <: Shape
Pentagon <: Polygon <: Shape, hence Pentagon <: Shape
Hexagon <: Polygon <: Shape, hence Hexagon <: Shape

Also:

Shape <: Shape

Therefore, every Circle object, for example, inherits position and size attributes from the Shape superclass.

Furthermore, in this example the Circle object will be able to access these attributes because they have protected or subclass scope as indicated by the "#" symbol.

Here's another example:

Note that our substitution semantics applies in all of these situations:

A jet can substitute for a plane which can substitute for a vehicle.

A hexagon can substitute for a polygon which can substitute for a shape.

However:

A jet can not substitute for a Car.

A hexagon can not substitute for an ellipse.

There are several things wrong with the following inheritance hierarchy:

Here's how it might be refactored into a better design:

Note that multiple inheritance is allowed in UML, even though this feature isn't supported in some object-oriented languages (like Java):