SCJP Study Guide:
OO Concepts


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs SCJP Study Guide: The 'is-a' and 'has-a' relationships

The 'is-a' and 'has-a' relationships


In the object-oriented world, every object can be described as a class. Every class has characteristics or states. For a good object-oriented design, not only need to define individual classes, but also need to manage the relationships among the classes. In the XyzWs SCJP Study Guide: Loose coupling and high cohesion , we talked about designing high cohesion classes and loose coupling between these classes. In the XyzWs SCJP Study Guide: Inheritance, we talked about that we can use extends existing classes to define new classes. Here we are going to talk about the 'is-a' and 'has-a' relationships.

The 'is-a' and 'has-a' relationships are the major relationships in object-oriented programming. There are other relationships such as 'behaves-like' and 'uses', which are not covered here. The meanimg of these relationships is exactly what it is in English.

  • Two things, X and Y are in the IS-A relationship simply if the phrase "X is a Y" or "all Xs are Ys" makes sense (and is true). Objects in an IS-A relationship are good candidates for inheritance as in class X extends Y. For example, A Duck is a Bird, so a Duck class might inherit from a Bird class.
  • Two things, X and Y are in a HAS-A relationship if Y is a property or componment of X, as in the English phrases "X has a Y", or "Y is a component of X". If X HAS-A Y, than Y will probably be an instance variable on class X. The HAS-A relationship does NOT indicate inheritance.

The relationships are the structure of the object. The 'has-a' relationship can be described in Java code as member fields. The 'is-a' relationship can be described in Java keyword extends. The 'is-a' relationship has an inheritance feature (like "extends" or "implements") and the 'has-a' relationship has an encapsulation feature (like private or protected modifier used before each member field or method).

Let's take a look at the following example:

The above figure shows the 'is-a' relationship. The 'is-a' relationship is also called generalization/specialization. The generalization/specialization association exists when one class is a specialized version of another class. In this relationship, a common, or base, class forms the foundation for more specialized, or derived, classes. This association is commonly referred to as inheritance because the derived classes inherit the functionality of their base classes to provide specialized behavior.

The 'is-a' relationship or inheritance provides the mechanism for new classes to be formed from the attributes and behavior of existing classes. As base classes are specialized into derived classes, hierarchies are formed that represent is-a or kind-of relationships. In our example, Spring is-a specialization of a Season; so is Summer. To identify this kind of relationship, just need to answer this this question: is this class a specialization of a more general class?

Let's take a look at another example:

This figure shows another kind of relationship, called 'has-a' relationship. The 'has-a' or 'composition' (also referred to as an 'associative') relationship captures the notion that one object has a distinct and persistant communication relationship with another object. In our example, a Tree 'has-a' Leave(s) and Root(s). The notion of '0..*' is a constrait, which means a Tree can have 0 to many Leave(s) and Root(s).


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |