Generalization vs Specialization

Generalization moves what classes share into one parent class, and specialization keeps what makes each child class different.

Apprentice · Java · OOP · Updated

By

Generalization

Generalization is the process of finding all variables and methods that each class has in common. We generalize these variables by extracting them into their own class.

The black variables and methods are what all classes have in common. They should be put into one “general” class from which the others can inherit. Since the Character class contains all of the common attributes, it is the ideal candidate to be the general class.

similar classes should extract common variables and methods

Specialization

Specialization is the process of finding the variables and methods that make a class different from all other classes.

For instance, an archer has arrows, a wizard has mana, and a knight has armor. These attributes differentiate the archer, the wizard, and the knight from each other and from all other characters.

Wizard and Knight as specialized classes

Cheat sheets