Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Editado a partir de Data Abstraction and Hierarchy, por Barbara Liskov, y Encapsulation and
Inheritance in Object-Oriented Programming Languages por Alan Snyder, por la ctedra de
Programacin Orientada a Objetos, curso 2004, UNLP
From the point of view of the resulting code, saying that one class is a subclass of another is
simply a shorthand notation for building programs. The exact program that is constructed
depends on the rules of the language, e.g., such things as when methods of the subclass
override methods of the superclass. The exact details of these rules are not important for our
discussion (although they clearly are important if the language is to be sensible and useful). The
point is that the result is equivalent to directly implementing a class containing the instance
variables and methods that result from applying the rules.
For example, suppose class T has operations o1 and o2 and instance variable v1 and class S,
which is declared to be a subclass of T, has operations o1 and o3 and instance variable v2. Then
the result in Smalltalk is effectively a class with two instance variables, v1 and v2, and three
operations, o1, o2, o3, where the code of o2 is supplied by T, and the code of the other two
operations is supplied by S. It is this combined code that must be understood, or modified if S is
reimplemented, unless S is restricted as discussed further below.
One problem with almost all inheritance mechanisms is that they compromise data abstraction to
an extent. In languages with inheritance, a data abstraction implementation (i.e., a class) has
two kinds of users. There are the "outsiders" who simply use the objects by calling the
operations. But in addition there are the "insiders." These are the subclasses, which are typically
permitted to violate encapsulation. There are three ways that encapsulation can be violated: the
subclass might access an instance variable of its superclass, call a private operation of its
superclass, or refer directly to superclasses of its superclass. (This last violation is not possible in
Smalltalk.)
When encapsulation is not violated, we can reason about operations of the superclass using their
specifications and we can ignore the representation of the superclass. When encapsulation is
violated, we lose the benefits of locality. We must consider the combined code of the sub- and
superclass in reasoning about the subclass, and if the superclass needs to be reimplemented, we
may need to reimplement its subclasses too. For example, this would be necessary if an instance
variable of the superclass changed, or if a subclass refers directly to a superclass of its superclass
T and then T is reimplemented to no longer have this superclass.
2.2. Implementation Hierarchy
The first way that inheritance is used is simply as a technique for implementing data types that
are similar to other existing types. For example, suppose we want to implement integer sets, with
operations (among others) to tell whether an element is a member of the set and to determine
the current size of the set. Suppose further that a List data type has already been implemented,
and that it provides a member operation and a size operation, as well as a convenient way of
representing the set. Then we could implement Set as a subclass of List; we might have the list
hold the set elements without duplication, i.e., if an element were added to the set twice, it
would be appear in the list only once. Then we would not need to provide implementations for
member and size, but we would need to implement other operations such as one that inserts a
new element into the set. Also, we should suppress certain other operations, such as
nextElementOf, to make them unavailable since they are not meaningful for sets. (This can be
done in Smalltalk by providing implementations in the subclass for the suppressed operations;
such an implementation would signal an exception if called.)
Another way of doing the same thing is to use one type as the representation of another. For
example, we might implement sets by using a list to contain its elements. In this case, we would
need to implement the size and member operations; each of these would simply call the
corresponding operation on lists. Writing down implementations for these two operations, even
though the code is very simple, is more work than not writing anything for them. On the other
hand, we need not do anything to take away undesirable operations such as nextElementOf.
2.3. Type Hierarchy
A type hierarchy is composed of subtypes and supertypes. The intuitive idea of a subtype is one
whose objects provide all the behavior of objects of another type (the supertype) plus something
extra. What is wanted here is something like the following substitution property (The Liskov
Substitution Principle): If for each object o1 of type S there is an object o2 of type T such that
for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted
for o2, then S is a subtype of T.
We are using the words "subtype" and "supertype" here to emphasize that now we are talking
about a semantic distinction. By contrast, "subclass" and "superclass" are simply linguistic
concepts in programming languages that allow programs to be built in a particular way. They can
be used to implement subtypes, but also, as mentioned in the previous section, in other ways.
We begin with some examples of types that are not subtypes of one another. First, a set is not a
subtype of a list nor is the reverse true. If the same element is added to a set twice, the result is
the same as if it had been added only once and the element is counted only once in computing
the size of the set. However, if the same element is added twice to a list, it occurs in the list
twice. Thus a program expecting a list might not work if passed a set; similarly a program
expecting a set might not work if passed a list. Another example of non-subtypes are stacks and
queues. Stacks are LIFO; when an element is removed from a stack, the last item added
(pushed) is removed. By contrast, queues are FIFO. A using program is likely to notice the
difference between these two types.
The above examples ignored a simple difference between the pairs of types, namely related
operations. A subtype must have all the operations of its supertype since otherwise the using
program could not use an operation it depends on. However, simply having operations of the
right names and signatures is not enough. (A operation's signature defines the numbers and
types of its input and output arguments.) The operations must also do the same things. For
example, stacks and queues might have operations of the same names, e.g., addElement to push
or enqueue and removeElement to pop or dequeue, but they still are not subtypes of one
another because the meanings of the operations are different for them.
Now we give some examples of subtype hierarchies. The first is indexed collections, which have
operations to access elements by index; e.g., there would be a at: operation to fetch the n-th
element of the collection. All subtypes have these operations too, but, in addition, each would
provide extra operations. Examples of subtypes are arrays, sequences, and indexed sets; e.g.,
sequences can be concatenated, and arrays can be modified by storing new objects in the
elements.
The second example is abstract devices, which unify a number of different kinds of input and
output devices. Particular devices might provide extra operations. In this case, abstract device
operations would be those that all devices support, e.g., the end-of-file test, while subtype
operations would be device specific. For example, a printer would have modification operations
such as putChar: but not reading operations such as getChar. Another possibility is that abstract
devices would have all possible operations, e.g., both putChar: and getChar, and thus all
subtypes would have the same set of operations. In this case, operations that not all real devices
can do must be specified in a general way that allows exceptions to be signalled. For example,
getChar would signal an exception when called on a printer.
An inheritance mechanism can be used to implement a subtype hierarchy. There would be a class
to implement the supertype and another class to implement each subtype. The class
implementing a subtype would declare the supertype's class as its superclass.
3. Type Hierarchy and Inheritance
3.1 Multiple Implementations
It is often useful to have multiple implementations of the same type. For example, for some
matrices we use a sparse representation and for others a nonsparse representation. Furthermore,
it is sometimes desirable to use objects of the same type but different representations within the
same program.
Object-oriented languages appear to allow users to simulate multiple implementations with
inheritance. Each implementation would be a subclass of another class that implements the type.
This latter class would probably be abstract; for example, there would be an abstract class
implementing matrices, and subclasses implementing sparse and nonsparse matrices. Using
inheritance in this way allows us to have several implementations of the same type in use within
the same program, but it interferes with type hierarchy. For example, suppose we invent a
subtype of matrices called extended-matrices. We would like to implement extended-matrices
with a class that inherits from matrices rather than from a particular implementation of matrices,
since this would allow us to combine it with either matrix implementation. This is not possible,
however. Instead, the extended-matrix class must explicitly state in its program text that it is a
subclass of sparse or nonsparse matrices.
The problem arises because inheritance is being used for two different things: to implement a
type and to indicate that one type is a subtype of another. These uses should be kept separate.
Then we could have what we really want: two types (matrix and extended-matrix), one a
subtype of the other, each having several implementations, and the ability to combine the
implementations of the subtype with those of the supertype in various ways.
addition to clients that simply instantiate objects of the class and perform operations on them,
there are other clients (class definitions) that inherit from the class. To fully characterize an
object-oriented language, we must consider what external interface is provided by a class to its
children. This external interface is just as important as the external interface provided to users of
the objects, as it serves as a contract between the class and its children, and thus limits the
degree to which the designer can safely make changes to the class.
Frequently, a designer will want to define different external interfaces for these two categories of
clients. Most object-oriented languages respond to this need by providing a much less restricted
external interface to children of a class. By doing so, the advantages of encapsulation that one
associates with object-oriented languages are severely weakened, as the designer of a class has
less freedom to make compatible changes. This issue would be less important if the use of
inheritance were confined to individual designers or small groups of designers who design
families of related classes. However, systems designers have found it useful to provide classes
designed to be inherited by large numbers of classes defined by independent applications
designers; such designers need the protection of a well-defined external interface to permit
implementation flexibility.
We will begin our examination of inheritance with the issue of access to inherited instance
variables.
Inheriting Instance Variables
In most object-oriented languages, the code of a class may directly access all the instance
variables of its objects, even those instance variables that were defined by an ancestor class.
Thus, the designer of a class is allowed full access to the representation defined by an ancestor
class.
This property does not change the external interface of individual objects, as it is still the case
that the instance variables of an object are accessible only to operations defined on that object.
However, it does change the external interface of the class (as seen by its descendants), which
now (implicitly) includes the instance variables.
Permitting access to instance variables defined by ancestor classes compromises the
encapsulation characteristics stated above: Because the instance variables are accessible to
clients of the class, they are (implicitly) part of the contract between the designer of the class
and the designers of descendant classes. Thus, the freedom of the designer to change the
implementation of a class is reduced. The designer can no longer safely rename, remove, or
reinterpret an instance variable without the risk of adversely affecting descendant classes that
depend on that instance variable. This is called the Fragile Base Class Problem.
In summary, permitting direct access to inherited instance variables weakens one of the major
benefits of object-oriented programming, the freedom of the designer to change the
representation of a class without impacting its clients.
Accessing Inherited Variables Safely
To preserve the full benefits of encapsulation, the external interfaces of a class definition should
not include instance variables. Instance variables are protected from direct access by users of an
object by requiring the use of operations to access instance variables. The same technique can
be used to prevent direct access by descendant classes.
Additional language support is required to permit instance variable access operations to be used