What is inheritance? - Part 5
This article explains inheritance through general-to-specific relationships, shop item examples, the is-a test, inherited behaviour and the reasons composition can be safer in practical systems.
What is inheritance? - Part 5
Inheritance is the most popular principle of object-oriented programming and, at the same time, the most misused one. Beginners love it because it looks like free reuse, write the code once in a parent class and every child class gets it. But many large systems become hard to change exactly because of inheritance, a small change in a parent class breaks ten child classes in different modules. Inheritance is not a code reuse trick, it is the same “general to specific” thinking we discussed in Part 2, a cardiac surgeon is a kind of surgeon and a surgeon is a kind of doctor. In the previous article, we discussed encapsulation and how an object protects its state. In this article, we are going to see what is inheritance, first in the real world and in philosophy, and then as a principle of object-oriented thinking, the “is a” test, what is really inherited, the common problems and when to prefer composition instead.
Object-Oriented Thinking series:
- How to model the real world with objects? - Part 1
- How do we organize what we know? - Part 2
- What is abstraction? - Part 3
- What is encapsulation? - Part 4
- What is inheritance? (this article)
- What are subtype and subclass polymorphism? - Part 6
- How objects relate to each other? - Part 7
- How all these concepts connect? - Part 8
TL; DR Summary
- Inheritance is a relationship where a specific concept is defined as a special kind of a more general concept.
- In the real world, inheritance is specialization, the special kind shares all the characteristics of the general kind and adds its own.
- It creates hierarchies of concepts and a clear relationship between them, “is a kind of”.
- A child inherits not only the characteristics of the parent, it also inherits its responsibilities and its promises.
- Use the “is a” test, a weighed item is an item, a bill is not a list.
- Changes in a parent affect all children, this is called the fragile base class problem. Keep hierarchies shallow.
- When the relationship is “has a” or “uses”, prefer composition over inheritance.
Common Challenges
Inheritance only for reuse
A developer sees that the Bill needs to keep a list of items and a list already has add, remove and count. So he makes the Bill a child of the List. It saves few lines of code. But now, anybody can call “remove all” or “insert at position 0” on the bill directly, without updating the total, and all the encapsulation we discussed in the previous article is gone. The bill is not a special kind of list, it just uses a list.
Hierarchies growing in every direction
It starts with Item, then PackagedItem and WeighedItem. Then someone needs PerishableItem. What about a packaged item which is perishable, like milk? PerishablePackagedItem. And an imported one? ImportedPerishablePackagedItem. Hmm, the tree is growing in every direction, each new combination needs a new class, and nobody remembers which class has which behaviour.
Inheritance in the real world and in philosophy
The word “inheritance” has more than one meaning in daily life, so let us first be clear about which meaning we are talking about.
- Heredity - A child inherits the eye colour and the height from his parents. But the child is not a kind of his father, he is a separate person.
- Legal inheritance - A son inherits the house of his father. The house moves from one person to another.
- Inheritance of a kind - A surgeon inherits all the duties of a doctor, because a surgeon is a kind of doctor.
Object-oriented thinking borrowed the words “parent” and “child” from the family, but the meaning comes from the third one, the classification we discussed in Part 2. Keep this in mind, many confusions about inheritance come from thinking of the family meaning. Let us discuss the different faces of this inheritance, without any computer.
Inheritance is a form of specialization
In Part 2, we saw that specialization is moving from a general kind to a special kind, by keeping everything of the general kind and adding what is different. Inheritance is the name for the “keeping everything” part.
The following diagram depicts the specialization of a profession. Every doctor diagnoses, prescribes and follows the medical ethics. A surgeon is a doctor who also operates, a physician is a doctor who treats with medicine. A cardiac surgeon is a surgeon who operates on the heart. Nobody needs to tell us that a cardiac surgeon can diagnose, he inherits it from being a doctor.

Inheritance is about sharing characteristics
Every vehicle on the road, a bicycle, a car or a bus, must follow the traffic signals. The traffic police doesn’t need separate rules for each brand of car, the rule is written once for “vehicle”, and every special kind shares it. It means, inheritance allows us to say a thing once, at the right level, and have it true for everything below.
Inheritance is about creating specialized versions
Let us take a South Indian kitchen. The plain dosa is the general one, made from a fermented batter of rice and urad dal. The masala dosa is a plain dosa with a potato filling. The onion dosa is a plain dosa with onion on top. Each special version keeps everything of the plain dosa, the batter, the way it is cooked on the hot tawa and the chutney on the side, and adds or changes one thing.
If the cook improves the batter recipe, every kind of dosa becomes better automatically. On the other hand, if the cook makes a mistake in the batter, every kind of dosa is spoiled. Hold this thought, it is exactly the fragile base class problem we will see later.
Inheritance creates hierarchies of concepts
When specialization is done again and again, we get the hierarchy we saw in Part 2, from “living thing” to “mango tree”, from “doctor” to “cardiac surgeon”. Each level inherits from the level above, and the higher the level, the more things share it. Whatever is true for every living thing is true for the mango tree, the sparrow and us.
Inheritance is a relationship between concepts
Inheritance is not about two things, it is about two kinds. “Bessie is a cow” is classification of a particular, “a cow is a mammal” is inheritance between two kinds. The relationship has a clear name, “is a kind of”, and it is different from “is a part of”. A cardiac surgeon is a kind of doctor, the heart is a part of the body. A heart is not a kind of body, and it doesn’t inherit anything from the body.
Promises are inherited too
When we visit a cardiac surgeon, we expect him to follow the medical ethics, keep our records confidential and act in our interest, simply because he is a doctor. He cannot say, “I am a surgeon, so the medical ethics don’t apply to me”. The same applies to a franchise restaurant. A franchise of a well known chain inherits the brand, the recipes and the hygiene standards. It can add a local item to the menu, but it cannot break the standards, otherwise the customer who trusted the brand is cheated.
It means, a special kind inherits not only the abilities, it also inherits the promises of the general kind.
Where inheritance breaks
We all learned in school “birds can fly”. Then we meet the penguin and the ostrich. A penguin is a bird, but it cannot keep the promise “can fly”. Either the promise was wrong for the general kind, or the penguin doesn’t belong there. Biologists solved it by not making “can fly” a characteristic of every bird. We will see the same problem in software in the next article, with the rectangle and the square.
So, what is inheritance in real life?
Based on the above, find below the faces of inheritance we have seen,
- Specialization - A special kind is made from a general kind (doctor to surgeon).
- Sharing characteristics - Say it once at the general level (traffic rules for every vehicle).
- Specialized versions - Keep everything, add or change one thing (the dosa family).
- Hierarchy - Repeated specialization forms levels (living thing to mango tree).
- Relationship - “Is a kind of”, not “is a part of” (surgeon and doctor, not heart and body).
- Promises - The duties and guarantees come down too (medical ethics, franchise standards).
Now, let us move to object-oriented thinking and see how this is used to design software.
What is inheritance in object-oriented thinking?
Inheritance is the relationship between a general concept and a more specific concept, where the specific concept takes all the characteristics and the responsibilities of the general one, and adds or refines its own.
In simple terms, inheritance is the “is a kind of” relationship. The general concept is called the parent, base or super class, and the specific concept is called the child, derived or sub class.
The following diagram depicts the inheritance in our shop. Every item has a name, a barcode and an amount, and each kind of item adds what is special to it. The weighed item also refines how the amount is calculated, weight multiplied by price per kg. The billing counter only asks the Item for its amount, it doesn’t know which kind of item it is talking to.

Generalization and specialization in software
In Part 2, we discussed generalization and specialization as the two directions of the same ladder. In software, we use both while designing a hierarchy,
- Generalization - Bottom up. We look at few specific concepts and find what is common. Packaged items, weighed items and perishable items all have a name, a barcode and an amount, so we generalize them as Item.
- Specialization - Top down. We start with a general concept and define the special kinds. An Item can be weighed, then it has price per kg and weight, and its amount is calculated as weight multiplied by price per kg. This is genus and differentia, an item (genus) which is priced by weight (differentia).
Both are ways to manage complexity. When the billing counter thinks “item”, it doesn’t care whether it is packaged or weighed. It just asks for the amount. The special details are handled by each kind. This is also abstraction, which we discussed in Part 3, at the level of a family of concepts.
The “is a” test
Before using inheritance, read the relationship as a sentence, “A [child] is a [parent]”, and check that it is true always, not only sometimes.
- A weighed item is an item. True.
- A perishable item is an item. True.
- A card payment is a payment. True.
- A bill is a list. False, a bill has a list of lines. This is the “is a part of” or “has a” relationship, not “is a kind of”.
- A customer is a person. True in English, but ask whether the software needs it. If a customer is the only kind of person in our system, the extra parent adds nothing.
Keep in mind that “is a” must be true for the behaviour also, not only for the name. Everything we can do with the parent must be possible with the child, without surprises, otherwise we have a penguin in a hierarchy which promises flying. We will see this in detail in the next article with the famous rectangle and square example.
What is inherited?
Most of the beginners think a child inherits only the fields and methods of the parent. Actually, like the cardiac surgeon, it inherits much more,
- State - The information the parent has, like name and barcode.
- Behaviour - What the parent can do, like calculating the amount.
- Responsibilities - The role the parent plays in the system, every item can be sold and billed.
- Promises - The rules the parent guarantees to its users, like “the amount is never negative”. The child must keep these promises, otherwise the users of the parent will be surprised.
It means, when we create a child, we are not just reusing code, we are making a promise that the child can be used wherever the parent is expected.
Problems with inheritance
Fragile base class
The child depends on the internal details of its parent, the same way every dosa depends on the batter. When the parent changes, even for a good reason, all the children may break. If the Item’s way of calculating the amount is changed to include tax, the WeighedItem which was adding its own tax is now charging tax twice. This is called the fragile base class problem, and it is a direct violation of the encapsulation we discussed in the previous article, inheritance opens the inside of the parent to its children.
Deep hierarchies
When the hierarchy has five or six levels, to understand one child, we have to read all its parents. The behaviour is spread across many classes, which method is actually called is not clear by just looking at the child. It is highly recommended to keep the hierarchy shallow, two or three levels are normally enough.
Combinations explosion
As we saw in the challenges, when concepts vary in more than one direction (packaged or weighed, perishable or not, local or imported), inheritance needs a class for every combination. It is the same as the first rule of a good classification in Part 2, divide by one principle at a time. Inheritance can model only one direction of variation well.
Composition over inheritance
Composition is building an object by combining other objects, the object “has” other objects and asks them to do part of the work. It is the “is a part of” hierarchy from Part 2.
The Gang of Four (Gamma, Helm, Johnson and Vlissides) in their book “Design Patterns” (1994) gave a famous guideline, favour object composition over class inheritance. It doesn’t mean never use inheritance, it means don’t use it as the first tool for reuse.
For our item, instead of a class for every combination, the item can have a pricing rule (fixed price or by weight), and optionally an expiry information and an origin. Now, milk is an item with fixed pricing and expiry information, loose rice is an item with weight pricing, and no new class is needed for each combination.
Find below the quick comparison,
| Inheritance | Composition | |
|---|---|---|
| Relationship | is a kind of | has a / is made of / uses |
| Decided | When the class is written | Can be changed while running |
| Coupling | − Child depends on parent internals | + Depends only on the other object’s behaviour |
| Variation | − One direction only | + Many directions, by combining parts |
| Simplicity | + Very simple for a true “is a” family | − Few more objects to design |
Again, it is a trade-off. Use inheritance when there is a true and stable “is a” relationship and the child can keep all the promises of the parent. Use composition when you need reuse, when things vary in many directions, or when you are not sure.
Wrap up
In this article, we have looked at inheritance in the real world and in philosophy, as specialization, sharing characteristics, specialized versions, hierarchies, relationships and promises, and then inheritance in object-oriented thinking, generalization and specialization in software, the “is a” test, what a child actually inherits, the problems of inheritance and why we should prefer composition in many cases. I appreciate your time and efforts to read this article. Inheritance gives us many kinds under one general kind, the next question is how to treat all of them the same way. In the next article, I will walk you through polymorphism, first as “one behaviour, many forms” in the real world, and then the difference between subtype polymorphism and subclass polymorphism, with code examples.
Related Posts
How all these concepts connect? - Part 8
This final article connects the three series, showing how real-world problems, computational thinking and object-oriented principles form one practical mental model for designing software with a grocery shop example.
How do we organize what we know? - Part 2
This article explains classification, generalization, specialization and hierarchy, showing how programmers organize grocery items and discover common concepts before designing software models for beginners today.
How to model the real world with objects? - Part 1
This article introduces object-oriented thinking by modeling a grocery shop as customers, items and bills, then explains objects through identity, state, behaviour and classes for beginners.