Class or Type Hierarchies



next up previous
Next: Overridingoverloading and Up: Mandatory features: the Previous: Types and Classes

Class or Type Hierarchies

Thine classes or types shalt inherit from their ancestors


Inheritance has two advantages: it is a powerful modeling tool, because it gives a concise and precise description of the world and it helps in factoring out shared specifications and implementations in applications.

An example will help illustrate the interest in having the system provide an inheritance mechanism. Assume that we have Employees and Students. Each Employee has a name, an age above 18 and a salary, he or she can die, get married and be paid (how dull is the life of the Employee!). Each Student has an age, a name and a set of grades. He or she can die, get married and have his or her GPA computed.

In a relational system, the data base designer defines a relation for Employee, a relation for Student, writes the code for the die, marry and pay operations on the Employee relation, and writes the code for the die, marry and GPA computation for the Student relation. Thus, the application programmer writes six programs.

In an object-oriented system, using the inheritance property, we recognize that Employees and Students are Persons; thus, they have something in common (the fact of being a Person), and they also have something specific. We introduce a type Person, which has attributes name and age and we write the operations die and marry for this type. Then, we declare that Employees are special types of Persons, who inherit attributes and operations, and have a special attribute salary and a special operation pay. Similarly, we declare that a Student is a special kind of Person, with a specific set-of-grades attribute and a special operation GPA computation. In this case, we have a better structured and more concise description of the schema (we factored out specification) and we have only written four programs (we factored out implementation). Inheritance helps code reusability, because every program is at the level at which the largest number of objects can share it.

There are at least four types of inheritance: substitution inheritance, inclusion inheritance, constraint inheritance and specialization inheritance.

In substitution inheritance, we say that a type t inherits from a type t', if we can perform more operations on objects of type t than on object of type t'. Thus, any place where we can have an object of type t', we can substitute for it an object of type t. This kind of inheritance is based on behavior and not on values.

Inclusion inheritance corresponds to the notion of classification. It states that t is subtype of t', if every object of type t is also an object of type t'. This type of inheritance is based on structure and not on operations. An example is a square type with methods get, set(size) and filled-square, with methods get, set(size), and fill(color).

Constraint inheritance is a subcase of inclusion inheritance. A type t is a subtype of a type t', if it consists of all objects of type t which satisfy a given constraint. An example of such a inheritance is that teenager is a subclass of person: teenagers don't have any more fields or operations than persons but they obey more specific constraints (their age is restricted to be between 13 and 19).

With specialization inheritance, a type t is a subtype of a type t', if objects of type t are objects of type t which contains more specific information. Examples of such are persons and employees where the information on employees is that of persons together with some extra fields.

Various degrees of these four types of inheritance are provided by existing systems and prototypes, and we do not prescribe a specific style of inheritance.



next up previous
Next: Overridingoverloading and Up: Mandatory features: the Previous: Types and Classes




Wed Apr 26 02:23:56 EDT 1995