Inheritance Vs Object Composition Java Source Code

Let’s start with what is Inheritance and what is Object Composition.

Inheritance is giving the characteristic of an object to the new object which inherited from the first object, as example is a man inherit eyes, hands, head, etc from human. In other way, we can say that inheritance is expanding or extending the parent object by the child object. Inheritance usually said “IS A“, man is a human. We see the implementation later…

Object Composition is inserting the whole object to the new object which created to contain the first object, as example is a computer contain of processor, RAM, Motherboard, etc. In other way, we can say that object composition is creating a new object from other objects. Object Composition usually said “HAVE A“, computer have a processor.

Then which is better, Inheritance or Object Composition? Both of them have some characteristic that give advantage to us. With inheritance, we can inserting the derived objects to their base object where Object composition cannot do that (this can be solve by Object class). But Object Composition can construct a new object from many objects where inheritance just allow one object to be inherited (this can be solve by interface).

Okay let’s see the implementation example. First we must define the base class…

public class RoboCore {

    private int coreID;

    public void setCoreID(int ID){
        this.coreID = ID;
    }

    public int getCoreID(){
        return this.coreID;
    }

    public void sayCoreID(){
        System.out.println(this.coreID);
    }

}

Then here is the code of inherit the base class

public class InheritedCoreRobo extends RoboCore{

    private String roboName;

    public InheritedCoreRobo(String Name, int CoreID){
        this.setCoreID(CoreID);
        this.roboName = Name;
    }

    @Override
    public void sayCoreID(){
        System.out.println("Core ID ["+getCoreID()+
                "] Inherited to "+this.roboName);
    }

    public void sayRoboName(){
        System.out.println("My Name is "+this.roboName);
    }

}

And here is the code for composing from base class

public class ComposedCoreRobo {

    private String roboName;
    RoboCore core;

    public ComposedCoreRobo(String Name, int ID){
        this.roboName = Name;
        this.core = new RoboCore();
        this.core.setCoreID(ID);
    }

    public void sayCoreID(){
        System.out.println("Core ID ["+this.core.getCoreID()+
                "] Composed to "+this.roboName);
    }

    public void sayRoboName(){
        System.out.println("My Name is "+this.roboName);
    }

}

I’ll also give you an example main source code for testing the code above.

    public static void main(String[] args) {

        InheritedCoreRobo ICRobo = new InheritedCoreRobo("ICRobo", 101);
        ComposedCoreRobo CCRobo = new ComposedCoreRobo("CCRobo", 777);

        System.out.println("Inheritance Robot");
        ICRobo.sayRoboName();
        ICRobo.sayCoreID();

        System.out.println("nObject Composition Robot");
        CCRobo.sayRoboName();
        CCRobo.sayCoreID();

    }

The output result is….

Inheritance Robot
My Name is ICRobo
Core ID [101] Inherited to ICRobo
Object Composition Robot
My Name is CCRobo
Core ID [777] Composed to CCRobo

The example above is show that inheritance can construct the object without creating any base object first. So we can imagine the inheritance is the RoboCore is extended by other stuffs to build a InheriteCoreRobo, but on Object Composition, we can imagine the ComposedCoreRobo which has slot for RoboCore is inserted with RoboCore object.

Back to above, inheritance “is a”, InheritedCoreRobo is RoboCore because the original version of InheritedCoreRobo just a RoboCore but with some modification and adding some stuff, it become InheritedCoreRobo. object composition “has a” because the ComposedCoreRobo is a stand-alone object which has a place for inserting RoboCore so we can say ComposedCoreRobo have a RoboCore.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation