Skip to main content

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("Engine Start");
}
}
class Body{
void type() {
System.out.println("Saloon Type");
}
}
class Ac{
void cool() {
System.out.println("Super Cool");
}
}

class Car{
Engine e;
Body b;
Ac ac;
Car () { e=new Engine();
         b= new Body();
         ac=null;        
}
Car (Ac p) { e=new Engine();
             b= new Body();
             ac=p;        
}
void specification(){
e.start();
b.type();
if (ac!=null)
    ac.cool();
}
}


class Post{
  public static void main(String a[]){
  Ac ac= new Ac();
  Car c1= new Car(ac)// car with AC
  c1.specification();
  Car c2= new Car()// car without AC
  c2.specification();
  }
  }


Output:

Engine Start
Saloon Type
Super Cool
Engine Start

Saloon Type


Same with Data Members

class Engine{
int hrp;
Engine () { }
Engine(int hp) { hrp=hp;}
void start() {
System.out.println("Engine Power "+hrp);
}
}
class Body{
int size;
Body()  {}
Body(int s){size=s;}
void type() {
System.out.println("Saloon Size "+size);
}
}
class Ac{
int price;
Ac(int p){price=p;}
void cool() {
System.out.println("Super Cool with price =" + price);
}
}
class Car{
Engine e;
Body b;
Ac  ac;
int price;
Car () { e=new Engine();
         b= new Body();
         ac=null;
}
Car (int p,int hp,int s,Ac a) { 
e=new Engine(hp);
b= new Body(s)
price=p; 
ac=a;
}
void specification(){
e.start();
b.type();
System.out.println("Car Price "+price);
if (ac!=null)
    ac.cool();
}
}

class Post{
public static void main(String a[]){
Ac ac=new Ac(1000);
Car c= new Car(1000,800,5,ac);
c.specification();

}
}


Output:

Engine Power 800
Saloon Size 5
Car Price 1000

Super Cool with price =1000

Comments

Popular posts from this blog

Generalization in java with examples

      Generalization basic points  Type/Subtype Relationship  "IS A " Relationship  One class has many subclasses   General class can have many Special classes as  subclasses  General names can be used to Address special names  Top to bottom hierarchy goes towards specialization  Bottom to top Hierarchy goes towards generalization e.g         Soap     Bath Soap and Washing Soap is a subclass of Soap e.g COldDrink   Pepsi      Team -Cold Drink is a general class -Pepsi and team are special classes e.g    Person Teacher       Student e.g            Car     Sedan           Hatchback Honda   Suzuki     Suzuki   Toyota civic        Liana      Alto      vitz ....................................................\ Some Examples from Nature e.g.               Animal            Mammals            Reptiles    Human           Cow     Snake         Croc Student Teacher             Cobra Python     e.g                     Fruits      Mango      

Composition in Java with code examples

Composition  "Have a " Relationship  OR  "Must have" relationship                    between two or more classes. Container class contains component's class object For example a  Car is composed of engine and body. Where Car is a container class and both Engine and Body are components class.  Life Time Issue in Composition Life time of component objects depends on container class If Container object destroyed, component objects will be also destroyed Car Example  There is a composition relationship between car's body and engine. Car must have Engine and body If Car destroyed, both Engine and Body will be destroyed class  Engine {   Engine  () {    System.out.println ( "Engine created" ) ; }   void  start () {    System.out.println ( "Engine Start" ) ;   } } class  Body {   Body  () { System.out.println ( "Body created" ) ; }   void  type () {    System.out.println ( "Sa