Friday, November 27, 2009

Inheritance (Classification) and Composition (Grouping) in Hierarchies

There are two modes to a hierarchy. Both these exist in the single hierarchy.

1. Inheritance (child is a type of parent - sphere is a type of shape)


2. Composition (parent has a child - gymnasium has a court)


Inheritance is virtual. Classes do not really exist. They generally describe objects. In Object Oriented Programming, classes are what objects are made from. In Zen, you are encouraged to avoid thinking about classes and concentrate on the real object as we usually see a tree and simply replace it with a symbol of a tree in our mind. I don't agree with Zen in this case because I think it is very important to think abstractly for efficiency and understanding connections. However, there are many times when we should remember to smell the roses.

Composition is the parts of something. A nice way to look at composition is nesting. We might also say that systems are made with composition. Composition is often real but it can also be virtual - like ideas composed of other ideas, etc. In Object Oriented Programming, composition can always do anything inheritance can do but not the other way around. So in coding, inheritance is just an efficiency (a way to avoid redefining properties) - again, abstract.