HAS-A, IS-A terminology in object oriented language
This is object-oriented programming and UML terminology, not Java-specific. There are actually three cases you should be aware of:
- A House is a Building (inheritance);
- A House has a Room (composition);
- A House has an occupant (aggregation).
The difference between (2) and (3) is subtle yet important to differentiate. Together they are forms of association. What's the difference? Composition implies the child object cannot live out of the context of the parent (destroy the house and rooms disappear) whereas aggregation implies the child can exist on its own (destroy the house and the occupant goes elsewhere).
A Car
has-a Wheel
.
A Sparrow
is-a Bird
.
Academically, the terms are used to decide between composition and inheritance.
Has-a means that the Class in question 'has a' field of a type.
Is-a means that the Class extends from a superclass or implements an interface. The instanceof
operator would return true
if tested against a class in the this case.
The names pretty much imply all.
An IS-A class can be thought of as specialized reference to an instance of another class that inherits all its attributes.
If a class called Vehicle exists. Then any type of vehicle may inherit the attributes of this super-class. For example Police Car will inherit all the attributes of Vehicle because it is a specialization of the latter.
On the other hand, an HAS-A class has a reference to another class or an instance of another class. In other words, it shares an association with another class. There are two types of HAS-A classes, aggregation meaning the class can exist independently, and composition, meaning the class can only exist along side the one it shares an association with.
It is important to know when to classify an object as a composition class or simply as an attribute of another class.
Taking the Vehicle Class again, a Driver class would be considered an aggregation because the Driver class can exist as an independent entity even if the Vehicle class ceases to exist.
Whereas an Engine class cannot exist if a Vehicle class is non-existent, because the Engine cannot exist outside the scope of the Vehicle.