Sunday, July 8, 2007

Generalization vs Specialization

In normal terms, they both refer to inheritance but the way in which it is implemented or looked into makes the difference. If many similar existing objects are combined to form a superclass to do the job of its subclass', then it is known as Generalization while if some new subclasses are created from an existing superclass to do specific job of the superclass, then it is known as specialization. Both concepts can be implemented using inheritance only. But the order of creation of the subclass and the superclass drives the concept name.


The way or perspective of looking also gives the difference between these two concepts. When you look from the perspective of a base class, it is generalization because the base class has functions or operations that may be commonly used by all of its subclasses. When you look from the perspective of the derived class, it is specialization because even though the common functionalities from the super class are available, the derived classes perform their own operations or functions specific to the purpose of their creation.






From the above figure, if you see from the perspective of the Train, then it is Generalization. The common functions or operations of both the Passenger and Goods Trains are put into the Train such as startTrain, stopTrain, fillFuel. That is, the generic operations of any subclass of Train are included in Train.



If you see from the perspective of the subclasses (Passenger Train or Goods Train), then it is Specialization. The common functions of the superclass (Train) are also included with the specific functions of the subclass such as boardPassengers, cleanToilet, fillWater, fillPantryResources in case of Passenger train and loadGoods, unloadGoods in case of Goods train making the subclass, a class for a specific or special function.


Note: In case of Generalization, it is not necessary that the superclass (Train) should know about the existence of its subclasses (Passenger Train and Goods Train). In case of Specialization, the subclass will always know its superclass.

22 comments:

Shameek said...

nice explaination...with a simple example ... keep it up

amit said...

nice explanation.........amit sharma.

vinit kumar said...

very well explained nice!!!

renu said...

good example...

deeje said...

really gud explaination

youngindia said...

wonderful example

vinay said...

it was good

pinks said...

very nice explanation good job

ramkrishnatiwary said...

nice explanation.........ramkrishna

vijaylaxmi said...

very nice explaination.......
But i need implementation of these two in C#.net....if possible plg give ....... vijaylaxmi

jacky said...

I was easy t understand the concept through this thanks.....

maahi said...

good explaination............
karamdeep kaur

priti pathak said...

good explanation.can u provide more details

Unknown said...

nice explaination...with a simple example ... keep it u

Unknown said...

nice explaination...with a simple example ... keep it u

Rameswar said...

Good one yaar...........

Rameswar said...

Good one yaar...........

santu said...

Very good explaintion with examples.

santu said...

Nice explaintion with example.

dayanand said...

Simple yet effective. Thanks :)

AMRB said...

Ahsan Mustafa Bhatti
very nice and useful. Great explanation.

Ashraf Hesham said...

it is really a very good and helpful explanation of the concept with a really simple example ! thanks a lot,,