Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
Hello friends, I hope you all are having fun. In today's tutorial, we will have a detailed Introduction to Inheritance in C#. It's our 15th tutorial in C# series and again an important concept in Object Oriented Programming, as it helps in code optimization. In our previous tutorial, we have discuss namespaces in C# and that was the last storage item in C#. Today, we are dealing with entirely different entity, which is inheritance. So, let's discuss it in detail:

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:
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • 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.
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • 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.
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • 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.
Now I hope you have understood How to use Inheritance in C#, so now we are ready to discuss a similar concept called Method Hiding, it's related to Inheritance so let's have a look at it.

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:
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • 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.
So, that was all about Inheritance in C#, which is quite simple concept but really effective in complex projects. It is considered as a pillar of Object Oriented Programming. In the next lecture, we will have a look at Polymorphism in C#. Till then, take care & have fun !!! :)