Specialization Generalization
Specialization Generalization
The greatest thing about object-oriented programming is code reuse. However, making code reusable isn’t
always the easiest to do. You have to use a different mindset when coding. One such option is
Generalization/Specialization, a term often used in UML.
Generalization/Specialization is taking something general and narrowing it down or starting specific and
going more general (Specialization/Generalization). When I say think of your dream car you instantly think
of the one car you would like to have. That is a specialization. If you were deciding on a realistic car to buy
you would ask yourself a number of questions. For example, Foreign or Domestic, Manufacturer, where will I
drive it, will I be the only one driving it, or will I need to carry other people in it (i.e. children)? You go from
the general idea of a "car" to the specialization of a specific "car."
When writing an application you may have objects that have a number of similarities or objects with
similarities over a number of projects. However, you may not necessarily see them. I’m not saying
everything has to be generalized or that everything can be but you should keep it in mind.
To start this process, generalize objects in your application or objects that you commonly use if you haven’t
already done so. For instance, our "car" has attributes like tires, doors, an engine, name and manufacturer.
Now if you think in the general sense basically all cars have these attributes. If you think even more general
then you know a car is an "automobile." Other types of automobiles like SUVs, buses and trucks also have
these attributes. If you were to list automobiles there would be no difference between a car and a SUV other
than its "type."
Now you have generalized some objects that you use. This is where object-oriented comes into the picture.
Generalization/Specialization would be basically impossible without the full options of object-oriented
programming, these being mainly inheritance and possibly interfaces. Although the syntax of each language
is different the theory of object-oriented programming is the same. You can start with Automobile as the
base class and define the common attributes there. Now you can define the type by extending Automobile
with Car, SUV, Truck, etc. Within the derived class (i.e. Car) you can add more attributes that are specific to
that type of automobile. Interfaces can come in handy for languages that do not allow multiple inheritance
(you can extend more than one base class) since most languages allow you to implement many interfaces.
You’re not quite finished yet. The idea behind this method is code reuse. When doing something general you
must remember that you should not access anything specific. For example, accessing something specific to
Car when working with Automobiles. Although it may still work, doing this could lower the code reuse. In
certain cases you may want to access something specific. If you do you should test the data type of the
object to the one you want and then cast it to that specific data type (if there is a match).
This is just an introduction to the Generalization/Specialization mindset. For more information, including
UML standards, here are a couple links you may find useful: Modelling Relationship in the Object Model and
Enhanced Entity Relationship Model (pdf).