|
Comp201: Principles of Object-Oriented Programming I
|
Summary: Relationships between objects can be classified as either "is-a" (inheritance) or "has-a" (composition). These two relationships enable the OO software designer to create abstract models of the desired system.
An object-oriented system can be characterized as a system of cooperating objects. Some objects interact only with certain other objects or perhaps only with a certain set of objects. Sometimes objects are treated as equivalent even though there may be specific differences between them, for instance a situation may call for a "fruit" whereupon an "apple" will do just as well as an "orange". That is, apples and oranges are treated as abstractly equivalent. Conversely, the system designer may want to express the commonality between apples and oranges. An OO system has two distinct mechanisms to express these relationship notions: "is-a" which is technically referred to as "inheritance" and "has-a" which is technically referred to as "composition".
UML Class Diagram Showing Inheritance![]() Figure 1: The above diagram shows the "is-a" relationship between Apple, Orange and Mango subclasses and the more abstract Fruit superclass. |
classes -- represented by boxes with the class name separated at the top by a horizontal line.
inheritance ("is-a") lines -- represented by solid lines with solid arrowheads. The arrow points from the subclass to the superclass (think "a subclass object is-a superclass object")
extends
keyword when declaring a
class. A subclass "extends" a
superclass, which means that the subclass
is a concrete example of the more abstract superclass. For instance,
the class Apple
would extend the class
Fruit
.
class Apple extends Fruit { ... }In Java, a subclass is allowed to extend only a single superclass (no "multiple inheritance"). This restricts the interpretation of a hierarchal taxomony. A subclass is an embodiment of its superclass. It is useful to think of the subclass as not inheriting its superclass's behaviors but rather possessing these behaviors simply because it is the superclass (this is polymorphism).
extends
really
models what an object intrinsically is--its true "being" as it were.
This is particularly useful when the superclass has particular
concrete behaviors that all the subclasses should exhibit. Note: You must use at least "Advanced Language Level" in DrJava for the following code!
public interface ISteerable { public abstract void turnLeft(); public abstract void turnRight(); } public class Truck implements ISteerable { public void turnLeft() { // turn the tires to the left } public void turnRight() { // turn the tires to the right } } public class Boat implements ISteerable { public void turnLeft() { // turn the rudder to the left } public void turnRight() { // turn the rudder to the right } }
public-- can be seen and used by anyone. Contrasts with private (seen and used only by that class) and package (seen and used only by classes in the same package). We'll talk more about these later.
abstract -- a purely abstract definition in that it specifies the existence of a behavior without specifying exactly what that behavior is.
void -- a non-existent return value, that is, no return value at all.
Above, Trucks and Boats are not taxonomically related, but yet they both embody the behaviors of steerability, such as the ability to turn left or right. A person can pilot either a boat or a truck based soley on the fact that they both support turning left or right and not based on what sort of entity they are fundamentally. Note that as per the above definition, a class can implement multiple interfaces at the same time.
UML Class Diagram Showing Implementation
![]() Figure 2: The above diagram shows the "acts-like-a" relationships between ISteerable, Boat, and Truck classes |
implementation ("acts-like-a") lines -- represented by dotted lines with solid arrowheads. The arrow points from the subclass to the interface (think "a subclass object acts-like-a interface")
UML Class Diagram Showing Composition
![]() Figure 3: The above diagram shows the "has-a" relationships between the Car, Motor and Wheel classes |
Last Revised Thursday, 03-Jun-2010 09:50:14 CDT
©2006 Stephen Wong and Dung Nguyen