Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » » » What Is HAS-A Relationship In Java??


When designing our classes , our basic tendency is to design classes maximum code re usability.  Reusable code means less work and maximum output. in this post(http://www.mydigitalvoice.in/2013/05/what-is-is-relationship-and-what-are.html) we have seen how to make code reusable by using extension.
We divide our classes into child and parent and then try to put check a IS-A relationship test.
http://www.mydigitalvoice.in/2013/05/cyclic-inheritance-prohibited.html  in this post I have shown what are the prohibited concept of java in terms of extension.
Now think about a scenario, I have a Tree class, I have Leave class and I have Root class as well.
Now Tree class has a methods like-
  • Consume_water();
  • Takes_CO2()
  • Release_O2()
  • Provide_shelter()
Again for Leaves class we can have the methods
  • Consume_water();
  • Takes_CO2()
  • Release_O2()
for Root class we can have  the methods..
  • Consume_water();
  • Provide_strength()
Now if you observe the methods of the classes(Tree,Leaves and Root) we can clearly say that Tree can be super class and Leave and Root can be subclass. By this approach we can get more reusable code.
so the structure could be for Tree class
class Tree{
public void Consume_water()
{
//consumes some water
}
Public void Takes_CO2()
{
//takes x amount of CO2
}
public int Release_O2()
{
//return some o2
}
}
For Leave class
class Leave extends Tree
{
//no methods are required as all will come from Tree
}

Similarly for Root class
class Root extends Tree
{
//no methods are required as all will come from Tree
public void Provide_Strength()
{
//provide some strength to tree
}
}

Looks like we are good to go!!!

But is it ok?? Surely no. The purpose of Tree is different from Leaves so as the corresponding methods. And to make a parent child relationship the child has to follow IS-A relationship.

Leaves IS(ARE)-A Tree?
or
Root IS-A Tree?

Both the answers are false. So this is not a valid parent child relationship. Now the next question is "if we can not extend Root or leaves to Tree , then how to reuse those codes?? if not possible the code redundancy will be higher . Isn't it?

To resolve such problem Java has come up with a concept called HAS-A Relationship.
Let us check the feature of HAS-A relationship

  • HAS-A relationship is also known as Composition or Aggregation 
  • For IS-A relationship there is a keyword available called "extends" but for HAS-A relationship there is no such keyword available,Mostly we use new.
  • Code re usability is the core concept for HAS-A relationship
Class Tree{
Leaves lvs=new Leaves();
//It means Tree has leaves reference
Root rt=new Root();
//It means Tree has Roots reference

Even though HAS-A relationship resolves problems of extension.At the same time it increases dependency between the underlying components.
It creates problem in maintenance of the code.


«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply