Question #31788

Distinguishing the differences from “has a” and “is a” relationships are key to understanding relationships between classes. Provide an example (include blocks of Java code) of both of these relationships that are within the interface or class header file for the same project / domain.
1

Expert's answer

2013-06-11T08:26:21-0400

When one class inherits from another, we say that the subclass extends the superclass. When we want to know if one thing should extend another, apply the IS-A test.

For example, Triangle IS-A Shape, that works; Cat IS-A Feline, that works too. Tub extends Bathroom, sounds reasonable. Until you apply the IS-A test.

To know if we have designed our types correctly, ask, "Does it make sense to say type X IS-A type Y?" If it doesn't, we know there's something wrong with the design, so if we apply the IS-A test, Tub IS-A Bathroom is definitely false.

What if we reverse it to Bathroom extends Tub? That still doesn't work, Bathroom IS-A Tub doesn't work.

Tub and Bathroom are related, but not through inheritance. Tub and Bathroom are joined by a HAS-A relationship.

Examples

IS-A: Cat is a Feline


class Feline {
...
}
class Cat extends Feline {
...
}


HAS-A: Bathroom has a Tub


class Bathroom {
    Tub newTub = new Tub();
    ...
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS