Generalization vs Specialization
Generalization moves what similar classes share into a base class; specialization keeps what makes each one different in its derived class.
Apprentice · C# · OOP · Updated
After this lesson you can
- Decide which members belong in a base class and which in each derived class
Key takeaways
- Generalization moves what every class shares, such as
Name,HealthandLevelUp(), up into the base class. - Specialization keeps what only one kind has in its derived class, such as
ManainWizardandArmorinKnight. - Ask of each member: does every kind of character have this? If only one kind has it, it goes in that derived class.
- A member every kind has, but some use differently, goes in the base class, and those derived classes override it.
Designing with inheritance takes two steps: generalize what the classes share, then specialize what makes each one different.
Generalization
Generalization is finding the properties and methods that every class has in common, and moving them into one general class: the base class.
The members in black are the ones every character shares. They belong in one general class, and Character is the natural home:

Generalized into Character:
- Properties:
Name,Health,Level,Experience - Methods:
Attack(),TakeDamage(),LevelUp(),SpecialAbility()
Specialization
Specialization is finding what makes each class different from the others, and keeping only that in its derived class.
An archer has arrows, a wizard has mana, and a knight has armor. Those members set each kind apart, so each stays in its own class:

| Class | Adds | Changes |
|---|---|---|
Archer |
Arrows, AddArrow() |
LevelUp(): also gets an arrow |
Wizard |
Mana, CastSpell(), RegenerateMana() |
LevelUp(), SpecialAbility() |
Knight |
Armor |
TakeDamage(): armor absorbs some of it |
Some members are shared and different: every character can level up, but an archer levels up a little differently. The shared version goes in the base class, and the derived class overrides it, the subject of Overriding Methods.
A quick check for each member
Ask of each property or method: does every kind of character have this?
- Yes, and it works the same for all: it goes in the base class.
- Yes, but it works differently for some: it goes in the base class, and those derived classes override it.
- No, only one kind has it: it goes in that derived class.
Check yourself
1 question to try on the lesson page.
So what
Generalize the shared parts up into the base class, specialize the differences down into derived classes. Get that split right, and each class stays small, with one clear job.