Skip to main content

Posts

Showing posts from August, 2013

Aggregation in java with example codes

What is aggregation?   "Have a" or "May have" relationship  Aggregation is special type of association  Life time of Component object is independent from its  container object E.g A school has one student class  Student  { String name; Student  () { name= null ;  } Student  ( String n ) { name=n;  } void  setName ( String n ) { name=n; } String getName () {  return  name; } } class  School { Student  st; School  (  ) {  st= null ; } School  ( Student s ){ st=s; } void  studentName (){    System.out.println ( st.getName ()) ; } } class  Post { public static  void  main  ( String a []){ Student st= new  Student ( "Imran" ) ; School s=  new  School ( st ) ; s.studentName () ; } } Output Hello Problem : A Car must has Engine and body.           A Car may or may not has Ac Adding Ac as an Aggregate object in car class  Engine { void  start () { System.out.println ( &