Third and last step on the inheritance: here to describe a situation where this mechanism is required, or better forced, to be adopted, in case we need to create object from a class.
What precisely does it mean?
We always have designed and realized classes with the clear intention to be used as direct prototypes for instantiation of objects.
And that's right; but if you remember we, by talking about inheritance in the two last articles, mentioned that when a method in a class is declared as virtual, it means that every child of the class can override that method, but never it will be forced to do.
Let's face the other side of the medal: how to make necessary a redeclaration by every child of a particular class?
But before the practice, what is the idea guiding us?
Try to think of a general class, so general that it cannot be directly turned into reality: no need to concentrate on informatics applications, the physical reality of every day can help us.
For example the concept of human being: we are in a situation where to infer the general features rising from the instantiations (ugly to say, but efficient) up to a possible class, good to collect every real human being.
Except for this, try to instantiate mentally a "human being" into reality: what happens?
Maybe the first and main question is: which is the sex?
It's so immediate that we don't have to dispute it!
Well... nobody forbids us thinking a method called SexSetting() which makes exactly what its name says: so in the class HumanBeing we write that method, declaring it as virtual.
Beware of it: simply virtual!
That's good: let's suppose to have completed the class with every possible and logical feature required.
You're now ready to instantiate it.
The scenario is the following: you have this class, and the classes Male and Female which inherits from the first, eventually redeclaring the SexSetting() method.
But what happens if even only one of the two inheriting classes doesn't redeclare?
We know that the father's method is automatically called to perform the instructions, and here the trap: which is the criterion to set the sex in a general HumanBeing class?
We could adopt a procedure to set it randomly, so that sometimes we get a male and sometimes a female... mmm, it doesn't sound good.
If we design a Male and Female class we surely want to instantiate a male and a female respectively, with a 100% certainty.
We have a problem... we cannot solve it into the father, because indeed is not possible to instantiate it, and we must hope that the inheriting classes are written with the special recommendation to redeclare this method.
And we're not going into crazy: thanks to the modularization we could hire someone to write the children classes in parallel with us writing the father. This means that our employee must be honest and follow our directives.