Ads Sponsored By Google :)

Saturday, 19 January 2013

Types|class|has-a|is-a|uses-a of relationships in java example


Types relationships in java relationships in java class relationships in java relationships in java-examples
relationships in java classes has-a relationship in java is-a relationship in java is-a relationship in java example
is a vs has a relationship in java relationships in java example examples of class relationships in java


  Types Of Relationships in java:

        The relationships in java makes us to understand how to use the features of one class into another class.
     In java programming we have three types of relationships. They are
           1)      Is –a relationship.
           2)      Has – a /  kind of / part of relationship.
           3)      Uses – a relationship.
        
         1)      Is – a relationship:
           Is – a relationship is one in which one class is inheriting the feature of another class through the 
        concept of inheritance with extends keyword.

   For Example:

     Class Base-Demo
   {
     System.out.println(“this is is-a relationship of Base Demo”);
    }
     Class Derived-Demo extends Base-Demo // Is – a relationship
   {
     System.out.println(“this is is-a relationship of Derived Demo”);
    }

     Analysis:
        From the above program representation of Is – a relationship by using extends keyword.
    Such that Is – a relationship would exists as Derived-Demo extends Base-Demo
      
      2)      Has – a relationship:
          Has – a relationship is one in which an object of one class is created as data member in another class.

   For Example:

        Class Base-Demo
      {
           System.out.println(“this is Has-a relationship of Base Demo”);
      }
       Class Derived-Demo
     {
       Base-Demo BD =new Base-Demo();//object creation of another class in this class as a data      
                                                               //member represents Has –a relationship.
       System.out.println(“this is Ha-a relationship of Derived Demo”);
      }

   Analysis: 
   From the above program representation of Has – a relationship object creation of another class in this 
   class as a data member represents Has –a relationship.

        3)      Uses – a relationship:
      Uses – a relationship in which a method of one class is using an object of another class.

     For Example:
         Class Base-Demo
       {
           System.out.println(“this is Uses-a relationship of Base Demo”);
        }

         Class Derived-Demo
         {
             Void Fun1()
             {
             Base-Demo BD =new Base-Demo();//this statement represents Uses – a relationship
             System.out.println(“this is Uses - a relationship of Derived Demo”);
             }
        }

    Analysis: 
     From the above program representation of Uses – a relationship such that in method Fun1() contains  
     creation of object of another class.
     
      The Following are the real time example for relationships their exists between classes.

     Note  1):  The  default relationship in java is Is – a relationship because for each and every class in 
                     java    their exists an implicit predefined super class called Java.lang.object.
                    Such that every user or developer defined class must  extends directly or indirectly.

   Note  2): System.out  is one the universal example for Has – a relationship (we known that out is an 
                   object of PrintStream class and it is create as static data member in System class).

   Note 3): The execution logic method of execution logic class is using an object of business logic class. 
                  Comes under the universal example of Uses – a  relationship.


No comments:

Post a Comment

LinkWithin