Back to blog

C#

Inheritance - Family in programming

Class Inheritance is based on connections between parent class (superclass) and child class (subclass). Child class is able to inherit and use parents’ class (public or protected) variables and methods as same as its own. It has few advantages like among others repetition avoidance what gives ability to create bigger systems without making mess or easy further system development without need to alter variables and methods trough all classes because shared instances are defined only once.

What is Class Inheritance?

Class Inheritance is based on connections between parent class (superclass) and child class (subclass). The child class is able to inherit and use parents’ class (public or protected) variables and methods as the same as its own. It has few advantages like among others repetition avoidance what gives the ability to create bigger systems without making mess or easy further system development without the need to alter variables and methods trough all classes because shared instances are defined only once. Inherited Class Example:

Image

InsuredPatient class is using Patients’ class public attributes instances (Name, Age, MoneyOwed and AmountOfTreatments) and also public operations (AddTreatment and PayTheBill). The InsuredPatient subclass is extending its functionality with two variables (companyName and percentageCover) and one method (ChargeInsurance) which are accessible only from objects with type InsuredPatient.

How to create inherited class structure?

Generalization is one the processes used to create inherited class structures of extracting shared characteristics from two or more classes, and combining them into a generalized superclass (Sourcemaking.com, 2017).

Image

(Sourcemaking.com, 2017)

Firstly, we have to determine which attributes are repetitive. These attributes will go to the top of the tree and will be elements of the parent class. Not-repetitive attributes will be moved to children classes.

In the example above, two classes (Piece of luggage and Piece of cargo) got combined into 1 superclass (Freight) and 2 subclasses. In contrast to generalization, specialization leads to creating new subclasses from an existing class (Sourcemaking.com, 2017).

If some of the class attributes apply only to a group of objects, we can divide class for superclass and two or more subclasses. Specialization is inverse of generalization.

Image

(Schardt and Chonoles, 2013)

If you have any questions, don't hesitate to contact me.

References

dzone.com. (2012). Polymorphism and Inheritance are Independent of Each Other - DZone Java. [online] Available at: ![Image](https://dzone.com/articles/polymorphism-and-inheritance">https://dzone.com/articles/polymorphism-and-inheritance [Accessed 30 Oct. 2017]. Sourcemaking.com. (2017). Design Patterns and Refactoring. [online] Available at: ![Image](https://sourcemaking.com/uml/modeling-it-systems/structural-view/generalization-specialization-and-inheritance">https://sourcemaking.com/uml/modeling-it-systems/structural-view/generalization-specialization-and-inheritance [Accessed 30 Oct. 2017].