Expressing Generalization Specialization In The UML


Three Purposes Of Inheritance

Inheritance serves three essential purposes. The first purpose of inheritance is to serve as an object-oriented design mechanism that enables you to think and reason about the structure of your programs in terms of generalized and specialized class behavior. A base class implements, or specifies, generalized behavior common to all of its subclasses. Subclasses derived from this base class capitalize on the behavior it provides. Additionally, subclasses may specify, or implement, specialized behavior if required in the context of the design.

When you think in terms of inheritance you think in terms of class hierarchies where the base classes that implement generalized behavior appear at or near the top of the hierarchy and the derived classes that implement specialized behavior appear toward the bottom. Figure 11-1 gives a classic example of an inheritance hierarchy showing generalized/specialized behavior.

image from book
Figure 11-1: Inheritance Hierarchy Illustrating Generalized & Specialized Behavior

Referring to figure 11-1 — the Auto class sits at the top of the inheritance hierarchy and provides generalized behavior for all of its derived classes. The Truck and Car classes derive from Auto. They may provide specialized behavior found only in trucks and cars. The DumpTruck and PickupTruck classes derive from Truck, which means that Truck is also serving as a base class. DumpTruck and PickupTruck inherit Truck’s generalized behavior and implement the specialized behavior required of these classes. The same holds true for the PassengerCar and SportsCar classes. Their direct base class is Car, whose direct base class is Auto. For more real-world examples of inheritance hierarchies simply consult the Java API documentation or refer to chapter 5.

The second purpose of inheritance is to provide a way to gain a measure of code reuse within your programs. If you can implement generalized behavior in a base class that’s common to all of its subclasses then you don’t have to re-write the code in each subclass. If, in one of your programming projects, you create an inheritance hierarchy and find you are repeating a lot of the same code in each of the subclasses, then it’s time for you to refactor your design and migrate the common code into a base class higher up in your inheritance hierarchy.

The third purpose of inheritance is to enable you to incrementally develop code. It is rare for a programmer, or more often, a team of programmers, to sit down and in one heroic effort completely code the entire inheritance hierarchy for a particular application. It’s more likely the case that the inheritance hierarchy, and its accompanying classes, grows over time. (Take the Java API as a prime example.)

Implementing The “is a” Relationship

A class that belongs to an inheritance hierarchy participates in what is called an is a” relationship. Referring again to figure 11-1, a Truck is an Auto and a Car is an Auto. Likewise, a DumpTruck is a Truck, and since a Truck is an Auto a DumpTruck is an Auto as well. In other words, class hierarchies are transitive in nature when navigating from specialized classes to more generalized classes. They are not transitive in the opposite direction however. For instance, an Auto is not a Truck or a Car, etc.

A thorough understanding of the “is a” relationships that exist within an inheritance hierarchy will pay huge dividends when you want to substitute a derived class object in code that specifies one of its base classes. (This is a critical skill in Java programming because it’s done extensively in the API.)

The Relationship Between The Terms Type, Interface, and Class

Before moving on it will help you to understand the relationship between the terms type, class and interface. Java is a strongly-typed programming language. This means that when you write a Java program and wish to call a method on a particular object, Java must know, in advance, the type of object to which you refer. In this context the term type refers to that set of operations or methods a particular object supports. Every object you use in your Java programs has an associated type. If, by mistake, you try and call a non-supported method on an object, you will be alerted to your mistake by a compiler error when you try and compile your program.

Meaning Of The Term Interface

An interface is a Java construct that introduces a new data type and its set of authorized operations in the form of method declarations. A method declaration specifies the method signature but omits the method body. No body — no behavior. Interfaces are discussed in more detail later in the chapter.

Meaning Of The Term Class

A class is a Java construct that introduces and defines a new data type. Like the interface, the class specifies a set of legal operations that can be called on an object of its type. However, the class can go one step further and provide definitions (i.e., behavior) for some or all of its methods. When a class provides definitions for all of its methods it is a concrete class, meaning that objects of that class type can be created with the new operator. (i.e., they can be instantiated) If a class definition omits the body, and therefore the behavior, of one or more of its methods, then that class must be declared to be an abstract class. Abstract class objects cannot be created with the new operator. I will discuss abstract classes in greater detail later in the chapter.

Quick Review

Inheritance serves three essential purposes: 1) it is an object-oriented design mechanism that enables you to think and reason about your program structure in terms of generalized and specialized class behavior, 2) it provides you with a measure of code reuse within your program by locating common class behavior in base classes, and 3) it provides a means to incrementally develop your programs over time.

Classes that belong to an inheritance hierarchy participate in an “is a” relationship between themselves and their chain of base classes. This “is a” relationship is transitive in the direction of specialized to generalized classes but not vice versa.

The Java class and interface constructs are each used to create new, user-defined data types. The interface construct is used to specify a set of authorized type methods and omits method behavior; the class construct is used to specify a set of authorized type methods and their behavior. A class construct, like an interface, can omit the bodies of one or more of its methods, however, such methods must be declared to be abstract. A class that declares one or more of its methods to be abstract must itself be declared to be an abstract class. Abstract class objects cannot be created with the new operator.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net