Saturday 3 August 2013

23:IS-A and HAS-A relationship

Using IS-A and HAS-A
When one class inherits from another, you say thatthe subclass extends the super class. When
you want to know if one thing should extend another, use the IS-A test. Examples for IS-A
relationship:
‰  Triangle IS-A Shape
‰  Green IS-A Color
When two classes are related, but not through inheritance, (for example, one class has a reference
to another class) then you say that the two classes are joined by HAS-A relationship. Examples for
HAS-A relationship:
‰  Bathroom HAS-A Tub
‰  Tub HAS-A Bubble
IS-A Relationship Test
The IS-A test works anywhere in the inheritance tree.
Do not apply inheritance if the subclass and super class do not pass the IS-A test. Always ask
yourself if the subclass IS-A more specific type of the super class.
Example:Tea IS-A beverage makes sense. Beverage IS-A tea does not make sense.
The IS-A relationship works only in one direction.
The IS-A relationship implies that if X IS-A Y,then X can do anything a Y can do (and possibly
more).
Examples:
‰  If class B extends class A, then class B IS-A class A.
‰  If class C extends class B, then class C passes the IS-A test for both B and A.
‰  Triangle IS-A Shape makes sense, so you can have Triangle extends Shape. But the
reverse, Shape IS-A Triangle does not make sense, so Shape should not extend
Triangle.
Try It Out
Problem Statement:
Write a program that illustrates the HAS-A relationship between Horse and Halter.
Code:
public class Animal { }
public class Horse extends Animal {
private Halter myHalter;
public void tie (LeadRope rope) {
myHalter.tie(rope); // Delegate tie behavior to the Halter
object
}
}
public class Halter {
public void tie(LeadRope aRope) {
// Do the actual tie work here
}
}
Refer Files: Animal.java, Halter.java, Horse.javato obtain soft copy of the program code
How It Works:
‰  A Horse IS-A Animal. A Horse HAS-A Halter.
‰  The following point summarizes the HAS-A relationship between Horse and Halter.
‰  Horse class has a Halter, because Horse declares an instance variable of type Halter.
‰  When code invokes tie() on a Horse instance, the Horse invokes tie() on the Halter
instance variable of the Horse object.
Tips and Tricks:
How do you know whether to make class, a subclass, an abstract class, or an interface?
Solution:
‰  Make a class that does not extend anything (other than Object) when your new class
does not pass the IS-A test for any other type.
‰  Make a subclass (in other words, extend a class) only when you need to make a more
specific version of a class and need to override or add new behaviors.
‰  Use an abstract class when you want to define a template for a group of subclasses,
and you have at least some implementation codethat all subclasses could use. Make
the class abstract when you want to guarantee that nobody can make objects of that
type.
Summary
‰  IS-A refers to inheritance.
‰  IS-A is expressed with the keyword extends.
‰  IS-A, “inherits from” and “is a subtype of” are all equivalent expressions.
‰  HAS-A means an instance of one class “has a” reference to an instance of another
class or another instance of the same class.
Test Your Understanding
1.State true or false for the following:
a)The IS-A test is applied to check whether one class inherits from another class.
b)Using HAS-A test, one can check whether a class has a reference to another
class.
2.Put a check next to the relationships that make sense (Hint: Apply IS-A test)
a)Guitar extends Instrument
b)Ferrari extends Engine
c)Metal extends Titanium
d)Container extends Jar

No comments:

Post a Comment