C #: from medium to advance level (Inheritance and Composition)

Matteo Lo Piccolo
4 min readAug 20, 2021

Now we talk about Inheritance, which is one of the PILLARS OF OOP.
But at same time, we talk about Composition.
In my little experience, especially at the beginning of my programming journey, I use A LOT of Inheritance and thought it was the BEST WAY to create code. Well, this is WRONG. Don’t misunderstanding, inheritance is really GREAT, but we need to watch out to something called Class Coupling.

When we create a small project, probably we don’t notice so much this type of problem.
The problem is when we try to make something little bigger, then the things start to be “not so good”.
This because when we use Inheritance, we create a reletion between Classes. And it’s ok, but if we create a big hierarchy of Classes dependant from THAT CLASS, it means every time we make one little change EVERYTHING IN THAT PART OF CODE ARE AFFECTED.

Let’s make some code to see this in action.
First : the concept of Inheritance is to have a relation between two Classes that allows one inherit some part of code of other.

Classic example from videogame.
We have Enemy. Every Enemy can move, can Attack, it has health and so on.
So, to start, I create Enemy Class.

Simple Enemy Class

This Class is a sort of base from every Enemy in our game.
For example, we can have an Ogre.
So I create an Ogre Class.

Now, to make this Class an Enemy-derived Class, the syntax is this

colon and name of base Class

In Unity, we need to DELETE MONOBEHAVIOUR, and substitute it with our custom Class.
This is because in C# we DON’T HAVE THE MULTIINHERITANCE.
This is really important, because some languages allow us to have multi inheritance, for example C++ or Phyton.
So remember, in C# we can derived only from one Class.

Now, after we use Inheritance, what we can do exactly?

Well, if in the Main program I create an instance of Ogre

If I access to ogre Class…

..we can access to Life, Move and Walk.
And obviously we can add some fields and methods only in Ogre Class.

This is the concept of Inheritance.
Is usually refer to it “is - a” relationship.
In this case, Ogre “is - a” Enemy.

I add a new method in Enemy Class

Now every Enemy can Move, Attack and Jump.
Then, we add new Enemy like Golem, Goblin.
At some point, we add Harpy, or Crow like in Castlevania game.

Crow and Harpy have Life, can Move, can Attack, but they don’t Jump, they Fly.

Of course, technically we can add Fly to Enemy and just not use Jump for Harpy and Crow. And it works. But logically, it’s incredibly WRONG.
Because we create a coupling when we don’t need it.

It seems a little problem in this example, but trust me, in a big project, or even a little big than small, this type a problem becoming something really bad to solve. Because every time we maka a change, everything change in conseguence. And it is not a great thing.

That’s why after this we talk about Composition.
Composition, know as “has — a” relationship, has different concept to Inheritance.
In Composition, we can create relationship between two Classes where one Class contain another.

I’ll talk more about Composition when we talk about Interfaces. Interfaces allow us to use Composition and are an essential part of Design Patterns.

But still, one step at a time.

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!