Composition Tutorial

The term composition is not unique to Java, composition is a concept of object-oriented programming. By now you should be familiar with the concept of inheritance - inheritance requires the use of the Java keywords extends or implements. When your class inherits members from either a superclass or a superinterface, you can directly invoke or access those members.
Consider the following code that demonstrates inheritance:
class OperatingSystem {
     void bootUp() { ... }
     void shutDown() { ... }
}
class Computer extends OperatingSystem {
}
class TestMe {
     public static void main(String args[]){
         Computer c = new Computer();
         c.bootUp();
         c.shutDown();
     }
}
In the example above how many objects are created in the statement Computer c = new Computer();? The answer is just one single instance of a Computer object - pretty basic.

Composition

In the real world objects are often composed of other objects. A computer is composed of many objects: case, motherboard, power supply, drive (ssd, platter, etc), memory, OS, etc. Composition, simply put, is when an object is composed of other objects.
Consider the following code that demonstrates composition:
class OperatingSystem {
     void bootUp() { ... }
     void shutDown() { ... }
}
class PowerSupply {
     void turnOn() { ... }
     void turnOff() { ... }
}
class Computer{
     private OperatingSystem os = new OperatingSystem();
     private PowerSupply ps = new PowerSupply();
     Computer() {
         ps.turnOn();
         os.bootUp();
     }
     void allDone() {
         os.shutDown();
         ps.turnOff();
     }
}
class TestMe {
     public static void main(String args[]){
         Computer c = new Computer();
         // use computer...
         c.allDone();
         c = null;
// critical for future aggregation tutorial
     }
}
In the example above how many objects are created in the statement Computer c = new Computer();? There are a total of three objects created after that statement executes. The computer object is the sum of all of its parts, in other words, the computer is composed of its parts - hence the meaning of composition. When the statement c = null; is executed I am destroying the computer object, since the computer is composed of other objects (OperatingSystem and PowerSupply) they are destroyed right along with the computer. There are no references pointing to the the Computer object, OperatingSystem object, or the PowerSupply object, so all three objects are eligible for garbage collection at that point. The concept of composition is a critical piece of the puzzle when I begin to introduce you to design patterns.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Composition
C:\Java>cd Composition
C:\Java\Composition>Notepad Composition.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Composition {
    public static void main(String args[]){
        Computer c = new Computer();
        System.out.println("\nBrowing the internet...");
        System.out.println("Writing some source code...");
        System.out.println("Saving work...\n");
        c.allDone();
        c = null; // critical for future aggregation tutorial
    }
}

class OperatingSystem {
    void bootUp() { System.out.println("OS is booting up"); }
    void shutDown() { System.out.println("OS is shutting down"); }
}

class PowerSupply {
    void turnOn() { System.out.println("Powering on"); }
    void turnOff() { System.out.println("Powering off"); }
}

class Computer {
    private OperatingSystem os = new OperatingSystem();
    private PowerSupply ps = new PowerSupply();

    Computer() {
        ps.turnOn();
        os.bootUp();
    }
    
    void allDone() {
        os.shutDown();
        ps.turnOff();
    }
}


Now switch back to the command prompt (CMD) and type in javac Composition.java and press Enter.
Now type in java Composition and press Enter.


C:\Java\Composition>javac Composition.java
C:\Java\Composition>java Composition
Powering on
OS is booting up

Browing the internet...
Writing some source code...
Saving work...

OS is shutting down
Powering off


Final thoughts

As you learn more about design patterns you will discover that composition is more versatile than inheritance and that you should use composition instead of inheritance whenever possible. Stay tuned for my next tutorial on Aggregation. Aggregation is very similar to Composition only with one very distinct difference.


Tutorials