Genericity is the key to reusability. Some classes are written with formal generic parameters representing arbitrary types. This form of parameterization is known as unconstrained genericity.
When genericity is combined with inheritance, the result is constrained genericity. In unconstrained genericity you have an arbitrary type, but in constrained genericity you have a type that is a descendant of a given class.
An object-oriented language that
supports genericity should support the constrained form. Table 1
shows some languages and indicates if they support genericity.
|
|
|
|
|
| No
(but extensive polymorphism) |
Yes | Yes
("templates") |
Yes |
Figure 2 shows an example of a generic sort procedure.
| generic
type T is private; -- T is of any non limited type type Range is (<>); -- Any discrete type type Vector is array ( Range) of T; with function ">" ( first, second:in T ) return Boolean is <>; procedure sort ( items: in out Vector); swap : Boolean := True; Temp : T; begin while swap loop swap := False; for I in items'First .. Range'Pred(items'Last) loop if items (I) > items (Range'Succ(I)) then swap :=True; Temp := items(Range'Succ(I)); items (Range'Succ(I)):= items(I); items (I) := Temp; end if; end loop; end loop; end sort; |
Go to Inheritance or the Table of Contents