5.5 IS-A & HAS-A RelationshipsHomepage  « Java5 Certification « 5.5 IS-A & HAS-A Relationships

In our final lesson within this section we investigate code that implements "is-a" and/or "has-a" relationships.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 5: OO Concepts

    • Develop code that implements "is-a" and/or "has-a" relationships.

IS-A and HAS-Ago to top of page Top

How do we know what to make a superclass or subclass and what goes into our instance variables for these classes. There is a very simple way to check, by using the IS-A and HAS-A test, to see what sort of relationships our classes and data members have. So how do we do this?

For the IS-A, anywhere where you would use the extends keyword just replace it with IS-A and see how the relationship looks.

For the HAS-A test, use the class followed by HAS-A and then the name of the possible instance variable and see how the relationship looks.

Try the following test to get to grips with IS-A and HAS-A relationships:

IS-A and HAS-A Test
Statement Question
Hammer extends Toolbox
  • valid
  • invalid
Hammer IS-A Toolbox - no.
Hammer HAS-A Toolbox - no.
Reversed
Toolbox IS-A Hammer - still no.
Toolbox HAS-A Hammer - yes.
Car extends Vehicle
  • valid
  • invalid
Car IS-A Vehicle - yes.
Car HAS-A Vehicle - no.
Sink extends Kitchen
  • valid
  • invalid
Sink IS-A Kitchen - no.
Sink HAS-A Kitchen - no.
Reversed
Kitchen IS-A Sink - still no.
Kitchen HAS-A Sink - yes.
Bird extends Canary
  • valid
  • invalid
Bird IS-A Canary - no.
Bird HAS-A Canary - no.
Reversed
Canary IS-A Bird - yes.
Canary HAS-A Bird - no.
Window extends House
  • valid
  • invalid
Window IS-A House - no.
Window HAS-A House - no.
Reversed
House IS-A Window - still no.
House HAS-A Window - yes.

Using IS-A and HAS-A are quite a simple way for checking inheritance relationships and the good thing is we can test for subclass suitability all the way down the inheritance tree.

Related Java5 Tutorials

OO Concepts - Inheritance Concepts

go to home page Java5 Tutor Homepage go to home page Top