kalit sikka
What is the Difference Between Inheritance and Composition
By kalit sikka in ASP.NET on May 30 2009
  • Prasoon
    Jun, 2009 11

    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.

     

     

    • 0
  • P Narasimha
    Jun, 2009 9

    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

    • 0
  • jabir eriyaden
    May, 2009 30
  • kalit sikka
    May, 2009 30

    What is the Difference between inheritance and Composition

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS