Results 1 to 4 of 4

Thread: Relationships in java

  1. #1
    Join Date
    Jan 2009
    Posts
    37

    Relationships in java

    Hi All

    Can anyone help me to understand the concept of relationship in java?
    What is 'Has a' and 'Is a' relationship of java? Can we apply relationships to the objects? What is the relationship between base and child class?

    Please give answers to my queries....

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Relationships in java

    Hi,

    The relationships in java are very easy to understand ,see the below details regarding this:

    "Is a" relationship:

    when one class inherits from another class this type relationship is "Is a" type.

    e.g. if parrot inherits from bird, we can say parrot is a bird.

    "Has a" relationship:

    when one object has a member variable which is a reference to another object.Then this type of relationship is called as "Has a" relationship in java.

    e.g. class monkey { leg monkeyleg; //here leg is another object }

    we can say a monkey has a leg.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Relationships in java

    "Is-a" OR "inheritance" relationships shows a hierarchical relationship between classes of objects.This relationships also referred as "generalization". For reference see the below example:

    An "animal" is a generalization of "lion", "cat", "monkey" and many others animals. Now We can say that animal is an abstraction of lion, cat, etc. we can also say that since lions are animal , in which they inherit all the properties which is common animals.

    "Has-a" OR "composition" OR "associative" relationships shows that one object has a distinct communication relationship with another object.
    For e.g, mango tree "has-a" tree.

    I hope this will useful for you..

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Relationships in java

    Hi, The following program will show you the relationship between base and child class in the java:

    #include <iostream>
    #include <stdlib.h>
    using namespace std;

    class BaseClass {

    public:
    void f(float);
    };

    void BaseClass::f(float price)
    {
    cout << "the base class!\n";
    cout << price * .075f;

    }

    class DerivedClass1:public BaseClass
    {
    public:
    void f(float);

    };
    void DerivedClass1::f(float price)
    {
    cout << "the child class 1\n";
    cout << price * .010f << endl;
    }

    class DerivedClass2:public BaseClass
    {
    public:
    void f(float);

    };

    void DerivedClass2::f(float price)
    {
    cout << "the child class 2\n";
    cout << price * .005f << endl;
    }

    int main()
    {
    DerivedClass1 myclass;
    DerivedClass2 anotherclass;

    myclass.f(100);
    anotherclass.f(100);

    return 0;
    }

Similar Threads

  1. Sim's relationships keep resetting in Sims 3: Generations
    By lEON bALDWIN in forum Video Games
    Replies: 5
    Last Post: 22-06-2011, 08:21 PM
  2. Relationships in The Elder scrolls
    By Hardiyy in forum Video Games
    Replies: 5
    Last Post: 06-06-2011, 11:02 PM
  3. How do i assign relationships primary key access
    By Kamran in forum Windows Software
    Replies: 3
    Last Post: 19-05-2009, 12:42 PM
  4. How many relationships can create in EJB?
    By Pandya in forum Software Development
    Replies: 2
    Last Post: 30-03-2009, 11:39 AM
  5. Database relationships problem
    By Barny in forum Software Development
    Replies: 4
    Last Post: 05-01-2009, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,016,710.58078 seconds with 17 queries