4
Reply

What is the Difference Between Inheritance and Composition

kalit sikka

kalit sikka

15y
24.4k
0
Reply

    In my opinion both concepts compliment each other but not necessarily same.

    Inheritance is basically extension while Composition is basically belonging.

    Take an Example of a House.

    A house will have Kitchen ,Bathrooms,BedRooms etc..

    So Kitchen,Bathroom,Bedrooms etc.. will share a composition relationship to Home as they are part of Room.

    Now a House has to have concrete walls so as to Kitchen ,bathroom and bedroom too.

    Now House can have 8 concrete Walls and Kitchen can have 4 concrete walls.

    Now Kitchen has inherited from House  that it should have concrete walls though number of walls differ.

    Programitically what it means that  int Numberofwalls() can be a method in class House  which will be inherited by class Kitchen.

    So there is some inheritance between class Kitchen and House.

    What i essentially trying to put my view here is that Composition and Inheritance can go side by side within some objects.

    If a object shares composition relationship then it can also share inheritance relationship with each other at the same time but with different methods.

    I will try to write a article on same (more from Developer's point of view) but hopefully reply has reduced your doubts.

    Cheers

    Prasoon.

     

     

    Hi,

    Inheritance is extends one class to anther class like

    Public class A

    {

    //Here methods and variable etc.

    }

    public class B:A

    {

    //here methods and properties variable etc..

    }

    public static void main()

    {

    B b = new B();

    b.method();

    b.Variable ..

    }

    here within what are the method and variable comes to base class B.

    composition is nothing but relationships between the different objects.

    some times Object made up from anther objects like Airplane is Wings,Landing gears ,engins etc.

    this relationship is called composition

    thanks,

    Narasima

    What is the Difference between inheritance and Composition