RELATIONSHIPS IN JAVA

Relationships


Relationships can be classified in to 2 types named as
    1.    is-A
    2.    has-A

1   is-A Relationship:

  • is-A Relationship shows the relationship between super class and sub class i.e,as follows.
  • javapadho
    Type of Constructor
  • The following figure shows the is-A relationship
    javapadho
    Type of Constructor
  • In the above fig. “B is-A A” but not “A is-A B” why because class B get the properties from class A but classA doesn’t get the properties from class B.

Example 1:

 
						
                    class A
                    {
                       
                    }
                    class B extends A
                    {
                       
                    } 
                        
  • In the above example” A is replaced with B but B is not replaced with A”

2   has-A Relationship:

  • has-A Relationship have the properties of the class.
  • javapadho
    Type of Constructor
  • It can be classified as two types. They are
        1.   Aggregation
        2.   composition

    1.   Aggregation :

  • Whenever whole disappears but a part appears.
  • The symbol of Aggregation is
    javapadho
    Type of Constructor

    i.e loosely coupled.

    2.   Composition :

  • whenever whole disappears then part also disappears is called as composition.
  • The symbol of composition is
    javapadho
    Type of Constructor

    i.e is tightly coupled

Example 2:

 
						
                    class Address
                    {
                       int doornum;
                       String street;
                       String city;
                       String state;
                       int pin;
                    }
                    class Person
                    {
                       public static void main(String args[ ])
                       {
                          Address add=new Address();
                       }
                    } 
                        

Explanation :

  • In the above example “Person has-A Address” but not Address has-A Person.
  • In the above Ex. Door num, street, city, state, pin are the properties of the Person.
  • i.e, Person has-A door num, Person has-A street,………..

Example 3:

Write a class for this following statements
“Person has-a Best Friend with Dog type .”
 
						
                    class Dog
                    {
                       
                    }
                    class Person
                    {
                       Dog bestFriend;
                    }