permaculture playing cards*
The moose likes Beginning Java and the fly likes HAS-A Relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HAS-A Relationship" Watch "HAS-A Relationship" New topic
Author

HAS-A Relationship

Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
can any body pls explain me HAS-A Relationship with example ? (i have tried k&B but it was not clear)


Thanks in advance.
regards
Amit Sethi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The HAS-A relationship refers to creating objects as member variables. As an example, let's say you are creating a graphical user interface (GUI) for your program. Your application requires a toolbar and a menu, so we create separate classes for these:


You also need a main window for your program. Clearly, a window HAS-A menu and a window HAS-A toolbar. We can represent this relationship by creating member variables in the Window class we need to create:

As you can see, the HAS-A relationship is not complicated. It is just a way to help us realize when we should create member reference variables.

Layne


Java API Documentation
The Java Tutorial
Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
Thanks Layne
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HAS-A Relationship