how to implement association relationship in Java

Discussion in 'Java' started by newsnet customer, Nov 4, 2006.

  1. Hi,

    I want to implement as association relationship in Java. I understand the implementation for this kind of relationship is called "containment". However, I can not find the code for this on the internet. From what is explained on the internet, I believe the code would look something like this:


    /*A Car 'has a' Wheel relationship*/

    public class wheel{
    public wheel(){
    }
    }

    public class Car{
    Wheel w;

    public Car(){
    w = new Wheel();
    }
    }

    Is this the basic idea?
    by the way, what does containment mean?

    ST
     
    newsnet customer, Nov 4, 2006
    #1
    1. Advertising

  2. newsnet customer

    hiwa Guest

    newsnet customer wrote:
    > what does containment mean?

    Container Car contains Wheel.
    Also you could use
    Hashtable<Car,Wheel>
    or
    HashMap<Car,Wheel>
    for representing association relationship.
     
    hiwa, Nov 4, 2006
    #2
    1. Advertising

  3. newsnet customer

    JanTheKing Guest

    Try an UML tool called JUDE (free) wherein you can create a simple
    class diagram to depict association relationship (fairly simple). Then
    click on "export to Java" to create the class files. That should
    clarify the concept.

    Cheers,
    Jan

    On Nov 4, 10:27 am, "hiwa" <> wrote:
    > newsnet customer wrote:
    > > what does containment mean?Container Car contains Wheel.

    > Also you could use
    > Hashtable<Car,Wheel>
    > or
    > HashMap<Car,Wheel>
    > for representing association relationship.
     
    JanTheKing, Nov 4, 2006
    #3
  4. newsnet customer

    LaieTechie Guest

    On Sat, 04 Nov 2006 02:46:04 +0000, newsnet customer wrote:

    > Hi,
    >
    > I want to implement as association relationship in Java. I understand the
    > implementation for this kind of relationship is called "containment".
    > However, I can not find the code for this on the internet. From what is
    > explained on the internet, I believe the code would look something like
    > this:
    >
    > /*A Car 'has a' Wheel relationship*/
    >
    > public class wheel{
    > public wheel(){
    > }
    > }
    > }
    > public class Car{
    > Wheel w;
    >
    > public Car(){
    > w = new Wheel();
    > }
    > }
    > }
    > Is this the basic idea?
    > by the way, what does containment mean?
    >
    > ST


    There are two types of relationships you should concern yourself with in
    Java:

    Has A: this indicates containment. In you example, a Car has a Wheel.
    Your container class should have a member property ("instance variable")
    of that type.

    Is A: this denotes inheritance. A Car is a vehicle. You indicate
    inheritance with "extends" (for classes) or "implements" (for interfaces).

    Public class Car extends Vehicle {}

    You may also run into "uses" relationships, which may translate into
    instance variables or local variables (variables defined within a certain
    scope, like a method).

    HTH,
    La`ie Techie
     
    LaieTechie, Nov 8, 2006
    #4
  5. Peace,

    I would like some help. As far as I've understood thus far:

    a "is a" b = inheritance of 'a' from super('b')
    a "has a" b = 'b' is a property of class 'a'

    now how can I understand the relationship of an interface to a class,
    as in:

    'a' implements 'b'

    Thanks in advance,
    God Bless,

    shree

    LaieTechie wrote:
    > On Sat, 04 Nov 2006 02:46:04 +0000, newsnet customer wrote:
    >
    > > Hi,
    > >
    > > I want to implement as association relationship in Java. I understand the
    > > implementation for this kind of relationship is called "containment".
    > > However, I can not find the code for this on the internet. From what is
    > > explained on the internet, I believe the code would look something like
    > > this:
    > >
    > > /*A Car 'has a' Wheel relationship*/
    > >
    > > public class wheel{
    > > public wheel(){
    > > }
    > > }
    > > }
    > > public class Car{
    > > Wheel w;
    > >
    > > public Car(){
    > > w = new Wheel();
    > > }
    > > }
    > > }
    > > Is this the basic idea?
    > > by the way, what does containment mean?
    > >
    > > ST

    >
    > There are two types of relationships you should concern yourself with in
    > Java:
    >
    > Has A: this indicates containment. In you example, a Car has a Wheel.
    > Your container class should have a member property ("instance variable")
    > of that type.
    >
    > Is A: this denotes inheritance. A Car is a vehicle. You indicate
    > inheritance with "extends" (for classes) or "implements" (for interfaces).
    >
    > Public class Car extends Vehicle {}
    >
    > You may also run into "uses" relationships, which may translate into
    > instance variables or local variables (variables defined within a certain
    > scope, like a method).
    >
    > HTH,
    > La`ie Techie
     
    Codedigestion, Nov 9, 2006
    #5
  6. newsnet customer

    Chris Uppal Guest

    Codedigestion wrote:

    > a "is a" b = inheritance of 'a' from super('b')
    > a "has a" b = 'b' is a property of class 'a'
    >
    > now how can I understand the relationship of an interface to a class,
    > as in:
    >
    > 'a' implements 'b'


    Unless you want to be pedantic, it's essentially the same as the 'is a' for
    class/subclass relationship. It tells you that an 'a' is acceptable wherever a
    'b' has been asked for.

    Still speaking a bit sloppily; the 'is-a' relationship should really be
    understood as 'can-be-used-as-if-it-were-a'. E.g: any Mammal can be used as if
    it were an Animal. Now with interfaces, what the interface does is specify
    what is /needed/ in order to be used as a <something>, so everything which
    implements that interface can be used as if it were a <something>.

    This could all be made more precise, but it would lead into a discussion of
    what wrong with (or at best misleading about) Java's class-based type system,
    and I don't think that would help at all just now.

    -- chris
     
    Chris Uppal, Nov 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MK
    Replies:
    2
    Views:
    2,501
  2. Mike
    Replies:
    6
    Views:
    1,145
  3. Alex Rast
    Replies:
    2
    Views:
    1,035
    Alex Rast
    Nov 10, 2004
  4. roadster

    implement association in C++

    roadster, Jun 16, 2004, in forum: C++
    Replies:
    2
    Views:
    9,652
    Rolf Magnus
    Jun 16, 2004
  5. Replies:
    1
    Views:
    693
    Tom Hawtin
    May 31, 2007
Loading...

Share This Page