Java code,java,Example,Tutorials,Syntax,Hibernate,Jsp, Servlet, FileHeadling,Java or Core Java Tutorial or Java Programming Tutorial is a widely used robust technology. learning of java from basic questions like what is java tutorial, core java, where it is used, what type of applications are created in java and why use java.

Search This Blog

Monday, 29 August 2016

Aggregation in Java

Aggregation in Java

If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Consider a situation, Employee object contains many informations such as id, name, emailId etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.

  1. class vipul{  
  2. int id;  
  3. String name;  
  4. Address address;//Address is a class  
  5. ...  
  6. }  

When use Aggregation?

  • Code reuse is also best achieved by aggregation when there is no is-a relationship.
  • Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved.
  • aggregation is the best choice.
Example
  1. class one{  
  2.  int square(int n){  
  3.   return n*n;  
  4.  }  
  5. }  
  6.   
  7. class two{  
  8.  Operation op;//aggregation  
  9.  double pi=3.14;  
  10.     
  11.  double area(int radius){  
  12.    op=new one();  
  13.    int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).  
  14.    return pi*rsquare;  
  15.  }  
  16.   
  17.      
  18.     
  19.  public static void main(String args[]){  
  20.    two c=new two();  
  21.    double result=c.area(5);  
  22.    System.out.println(result);  
  23.  }  
  24. }  
Share:

Agravat &Google

Total Pageviews

Blog Archive

vipul c.Agravat . Powered by Blogger.

myjava

myjava
java

myimg

myimg
good

Translate

Recent Posts

Unordered List

Definition List

Pages

Theme Support