Composite

Declare reference type variable as class member.

class Circle {
    //composite Point
    Point p = new Point();
    int r;
    
    Circle () {}
}

class Point {
    int x;
    int y;
}

Circle c = new Circle();
c.r = 0;
c.p.x = 1;
c.p.y = 2;

make small group of class, and then make class by combinatopm pf these classes.

class Car {
    Engine e = new Engine();
    Door[] d = new Door[4];
    //...
}

How to choose relation between inherit or composite?

  1. inherit : A is B.

  2. composite : A has B. 90%

ex) Circle is Point. < Circle has Point.(Composit relation!)

Last updated