It’s time to think about the family! Or Inheritance in C#

Remembering my post two weeks ago (What is the object …) I could not miss the theme of inheritance in programming. It is one of the basic principles of object oriented programming. Here is a list of all those principles according to wikipedia:

Encapsulation – property of the system, allowing you to combine data and methods that are working with data in the class. Also, it is sometimes identified as hiding.

Polymorphism – property of the system to use objects with the same interface without information about the type and internal structure of the object.

Inheritance – property of the system, allowing to describe a new class based on an existing partially or completely borrowing its functionality. Class, from which the inheritance is produced, called the base, the parent or superclass. A new class – the descendant, heir, a subsidiary or a derived class.

But for now lets forget about the first two frightening words and consider the inheritance using the example based on allegory.

Let us remember our friend – Gingerbread Man Happy, good guy isn’t he? But since he is a cookie, its lifetime is limited however sad it might be 🙁 And it is time for him to think about the family and the receiver of his duties of the King. Yes, yes, our friend is a King of Cookie country.

So, a wife and a first-born child our Happy guy already have, but what daddy needs to teach his son? The young prince should be able to know as much as his father knows, but he has to be even better! Daddy sent his son to school where he learned how to Count! Therefore the son became wiser than the father! And he will be a wonderful new king for the Cookie country! Happy end of this little story!

Let us now consider all this with the code. Here is the the class to create any ginger man who also has a function of greeting.

4.1 - Copy

This is the code to create an object GingerBreadMan named Happy (our father-king) and the testimony of its work function when he greets you:

4.2 - Copy

Now, for the creation, our improved Cookie-prince we need to create a new class. Because the father-king gave all his knowledge and abilities to the son, we will use the principle of inheritance as it allows creation of derived classes, based on all of the methods and elements of the base class (parent class). At the same time, we can add the necessary code in a derived class to improve the program: to add new elements, methods, etc. The base class will remain intact.

The code below is the creation of a derived class GingerBreadManCounts for all cookies that learned how to count. Here we show the parent class, the usage of the constructor that determines all variables from the class of the parent and creation of the new method that the object could count.

4.3 - Copy

In the following code, you should pay attention only to the code to create the Prince, which occurs in the same way as the creation of the Father King.

4.4 - Copy

Also in the example that were used, some manipulation is not yet clear, but I will just explain that each time you press the button on the form (below) Prince counts +1.

4.5 - Copy

The son learned to count even further:

4.6 - Copy

We should also note that earlier in the example in ССЫЛКА Article 2  all the variables have that GingerBreadMan had had a type – private. But we had to change the type to protected for variables from the parent class to be able to use in a derived class. To bring a better understanding I’ll put here a table of types for the class:

private protected public
Access from the class body  open  open   Open
Access from the derived classes  closed   open   Open
Accessing external functions and classes  closed  closed   open


When you work with classes, there are some rules for working with types:

1) Variable mostly must be private (only if it is not a parent class)

2) constructors and methods is likely to be public, unless the method is to be used outside the class, and constructors are always used outside class.


I hope this view is understandable. If not, I’m ready to explain my vision in the comments.

© 2024 Helena Boitsova