A generalization is a relationship between a general thing (called the superclass or parent) and a more specific kind of that thing (called subclass or child) [BRJ99]. Generalization is the relationship that represents the mechanism of inheritance in object-oriented languages.

Inheritance is often considered as one of the most fundamental features of the object-oriented paradigm. It is certainly the feature that distinguishes object-oriented from the traditional programming. Inheritance was introduced to the world of programming in the late 60s as the main feature of the programming language SIMULA [DMN68]. SIMULA'S inheritance mechanism was originally known as concatenation and the term inheritance was introduced a few years later. Currently, there are a few used synonyms for inheritance, such as subclassing, derivation, or subtyping.

The central idea of inheritance is straightforward. Inheritance allows new object definitions to be based upon exiting ones. A formal definition of inheritance is given by [BC90]:

P are the properties inherited from an existing class and dR are the new properties defined in the new class R. dR represents the incrementally added properties in class R that make class R different from class P. The symbol + is some operator that combines the exiting properties with the newly added ones. Inheritance is a facility for differential, incremental program development [Tai96]. Class P is referred to as superclass, parent, or ancestor and class R is referred to as subclass, child, or descendant. A subclass inherits attributes and methods from the superclass, and therefore inherits data and behavior from the superclass. As such, a subclass can substitute the superclass anywhere the superclass appears, but not vice versa.

If an operation defined in the subclass has the same name and parameters or signature as the one defined in the superclass, then the operation of the subclass overrides the operation of the superclass. This phenomenon is known as polymorphism.

A subclass can even cancel an operation defined in the superclass. This can be achieved by simply redefining the same operation and not providing any logic for it. Generally a subclass may introduce new properties in addition to the ones defined in the superclass that extend, modify, or defeat them [Tai96]. The UML notation for the generalization is shown in Figure 4-16.

Figure 4-16. UML notation for the relationship of generalization.

In Figure 4-16, perennial plants are a special case of plants and annual plants are a special case of plants, but perennials are different from annual plants. A perennial is a kind of plant, a specific kind of plant. A plant considered randomly may not necessarily be a perennial. The generalization relationship expresses a certain hierarchy of objects; moving up in the hierarchy objects become more general and moving down in the hierarchy objects become more special. Any object at a lower level can replace an object residing higher in the hierarchy.

Inheritance is the mechanism by which more-specific elements incorporate the structure and behavior of more-general elements [BRJ99]. Both terms, generalization and inheritance, are generally interchangeable but there is a clear distinguishing between them. Generalization is the name of the relationship, whereas inheritance is the mechanism that the generalization relationship represents.

In Figure 4-17, a hierarchy of different classes related by inheritance is shown [AR97]. The definition of class ShootOrgan includes a number of attributes that will be inherited by Stem, MainStem, and BranchStem. For example, age is an attribute defined at the ShootOrgan class and classes Stem, MainStem, and BranchStem will have an attribute with the same name although it is not shown in their attribute compartment. A subclass shows only the attributes defined at subclass level. Therefore, objects created by MainStem class will have attributes defined by ShootOrgan, Stem, and MainStem classes. The list of attributes of class BranchStem will contain on_stem_number, location_onStem, length, and number_leaves_on_stem defined in the abovementioned classes.

The author, [AR97], has chosen not to provide any behavior for classes ShootOrgan and Stem. Therefore, objects created by class MainStem will not inherit any behavior from the superclasses ShootOrgan and Stem.

Figure 4-17. Example of a hierarchy of classes related by inheritance.

Generalization is transitive; class Stem inherits from class ShootOrgan and class MainStem inherits from Stem. Therefore, class MainStem inherits from class ShootOrgan.

Another example of inheritance is taken from Lemmon [LCh97]. Classes shown in Figure 4-18 are all organs, as they inherit directly or indirectly from class Organ. Class Leaf directly inherits data and behavior from class Organ. Classes SympodialLeaf] PreFruitingLeaf and FruitingNodeLeaf inherit data and behavior from class Leaf and at the same time, they inherit from class Organ. A PreFruitingLeaf object is a specialized kind of Leaf and a specialized kind of Organ.

Figure 4-18. Hierarchy of classes in a cotton simulation model.

As previously mentioned, subclasses inherit from superclasses data and behavior. Figure 4-19 shows an example of what is inherited through a generalization relationship.

Figure 4-19. A subclass inherits from the superclass attributes, operations, and relationships.

An AutomotiveMachine has two attributes: licenseNumber and weight and an operation named registerQ. Class AutomotiveMachine is related with class Farmer through an association where the role of the farmer is owner.

Class Truck has its own attributes that are model and year and two inherited attributes from AutomotiveMachine that are licenseNumber and weight. Truck does not have any operations on its own but it does inherit from AutomotiveMachine operation registerQ. Class Tractor has the attribute power and two inherited ones, licenseNumber and weight.

Because of the generalization relationship, Truck and Tractor are related to a Farmer. Tractor is related to a Plow as well. Through the inheritance mechanism, a subclass inherits from the superclass attributes, operations, and relationships.

When a class inherits from only one class, then the inheritance is referred to as single inheritance. Single inheritance is the most common mechanism of inheritance used. When a class inherits from more than one class, then the inheritance is referred to as multiple inheritance. Multiple inheritance offers more possibilities for incremental modification than the single inheritance, but its use is not easy. Almost unanimously, researchers agree that the use of multiple inheritance should be done with care as its use introduces technical and conceptual problems. Some authors state that despite the problems the use of multiple inheritance raises, any modern object-oriented language should provide support for it [Taiv96]. Other researchers do not agree; they state that multiple inheritance is dangerous and should not be used. A strong support up for the argument against the use of multiple inheritance can be found in the lack of its implementation in two of the modern object-oriented languages; Java and C# provide a single inheritance mechanism while C++ provides support for multiple inheritance. Multiple inheritance is good, but there is no good way to do it [Coo87].

All examples presented in this section are examples of single inheritance. Figure 4-20 shows an example of multiple inheritance. As shown in this figure, class Segment inherits data and behavior from both classes Article and Organ [DP01].

Figure 4-20. Class Segment inherits from both classes Article and Organ.

Was this article helpful?

0 0

Post a comment