
Introduction to Inheritance in C#


Introduction to Inheritance in C#
- Inheritance in C# is used to create a relation between two classes. In simple words, derived / child class is inherited from base / parent class and will automatically have all its members as its own. Inheritance is denoted by ( : ).
- Mostly in projects, there are few functions which we have to use a lot throughout the project, so we can place such functions in any parent class and then which class needs those functions, we can simply inherit it from that parent class.
- Here's the syntax for inheritance, where ChildClass is Inherited from ParentClass, so ChildClass can use all functions and fields of ParentClass:

- When we create a new instance of any child class, then parent class constructor executes first and then child class constructor executes.
- So, in below figure, I have created two classes, ChildClass is inherited from ParentClass and I have also created their Constructors.
- Mow in the Main function, I have created a new instance of ChildClass and you can see in the Console that first Parent Constructor Called & then Child Constructor Called.

- Suppose, you are designing a software for college class, where students have few same subjects but some different subjects.
- Let's say, Engineering students study Physics & Chemistry while Medical students study Biology but they all have to study Mathematics & English.
- So, I have created one Parent Class with name MainSubjects, which has the compulsory subjects in it.
- Then I have created two Derived Class named MedicalSubjects & EnggSubjects and I have derived both of them from Main Subjects, so they can use both Maths & English fields.

- You can see inheritance in above figure, in the definition of class we have MedicalSubjects : MainSubjects, so MedicalSubjects is inherited from MainSubjects.
- In the Main code, I have created new instance of MedicalSubjects & then updated score for English, which is actually in MainSubjects, and it updated correctly.
- So, I can use all members of parent class in derived class.
- The benefit of Inheritance is that, it allows user to reuse old code without writing it again, so it saves time and reduce errors.
- C# allows single class inheritance i.e. we can't derive class A from two different classes at the same time.
- C# allows multiple Interface inheritance, which we will study later in detail.
Method Hiding in C#
- Method Hiding in C# is a simple technique, where we declare two methods with the same name, one in Child Class & other one in Parent Class and the Child method will hide the parent Method, if called from Child Class Reference Variable.
- We need to use new Keyword with the child class Method, which will hide the old Parent class method.
- In our previous part, we have seen How to use Inheritance and have created Parent & Child class.
- Now, let's create two Methods with the same name, one in ParentClass & one in ChildClass, as shown in below figure:

- You can see in above figure, that I have created a Method PrintMsg() in both ParentClass & ChildClass.
- In the Main Function, I have created 3 reference variables, which are:
- PC is a ParentClass variable, pointing to ParentClass Object.
- CC is a ChildClass variable, pointing to ChildClass Object.
- PC2 is a ParentClass variable, pointing to ChildClass Object.
- You must be wondering about the third variable, yeah we can do that as we can use all members of ParentClass in ChildClass but we can't do the opposite.
- Moreover, when we called the PrintMsg() Function from 2nd variable, which is a ChildClass variable, then it has executed the Function in ChildClass, which has new Keyword in its definition.
- So, the second variable has completely ignored (hide) the method in ParentClass and simply used the new method in ChildClass and it's called Method Hiding.
×
![]()