Inheritance in C#

Inheritance

Acquiring the properties of an existing class into a new class by establishing a parent–child relationship between classes is known as Inheritance.

The big advantage of inheritance is reusability of code:

inheritance

Syntax

[<Modifier>] Class <CC Name> : <PC Name>

Where

CC: child classs
PC: Parent class.

For example:

  1. Class class1  
  2. {  
  3.    --members  
  4. }  
  5. Class class2:class1  
  6. {  
  7.    Consume member of parent  

Note: private members of a parent class can never be consumed by child classes.

For example:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace InheritanceDemo  
  8. {  
  9.     class Parent  
  10.     {  
  11.         public void ParentMethod()  
  12.         {  
  13.             Console.WriteLine("this is parent method");  
  14.         }  
  15.     }  
  16.     class child :Parent  
  17.     {   
  18.         public void childmethod()  
  19.         {  
  20.             Console.WriteLine("this is child method");  
  21.         }  
  22.         static void Main(string[] args)  
  23.         {        
  24.             child obj = new child();                            
  25.             obj.childmethod();  
  26.             obj.ParentMethod();  
  27.             Console.ReadKey();  
  28.         }  
  29.     }  

The following are the rules and regulations that must be adopted when working with inheritance.

Rule 1:  if inherited, the execution of any child class starts by invoking its parent class's default constructor by default.

So the constructor of the parent class must be accessible by the child class or else inheritance will not be possible.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace InheritanceDemo  
  8. {  
  9.     class Parent  
  10.     {  
  11.          public Parent()  
  12.         {  
  13.             Console.WriteLine("Parent Constructor");  
  14.         }     
  15.         public void ParentMethod()  
  16.         {  
  17.             Console.WriteLine("this is parent method");  
  18.         }  
  19.     }  
  20.     class child :Parent  
  21.     {  
  22.         public child()  
  23.         {  
  24.             Console.WriteLine("this is child construcotor");  
  25.         }  
  26.     
  27.         public void childmethod()  
  28.         {  
  29.             Console.WriteLine("this is child method");  
  30.         }  
  31.         static void Main(string[] args)  
  32.         {        
  33.             child obj = new child();                            
  34.             obj.childmethod();  
  35.             obj.ParentMethod();  
  36.             Console.ReadKey();  
  37.         }  
  38.     }  

Rule 2: In inheritance the child class can access its parent class's members but a parent class can never access its child class member.

To test this rewrite the code under the main metod of class2 as follows.

  1. static void Main(string[] args)  
  2. {  
  3.     Parent p=new Parent ();  
  4.     p.ParentMethod ();//valid  
  5.     p.childmethod();//invalid  

Rule 3: In the same way an object of a class can also be assigned to its parent class variable and make it as a reference, but in this scenario we are also using the parent reference. We cannot access only a member that is purely defind in the child class even if the reference is consuming the same object memory.

To test the rewrite of the code under the child class of the main method is as follows:

  1. static void Main(string[] args)  
  2. {       
  3.     child obj = new child();  
  4.     Parent p=obj;  
  5.     p.ParentMethod();//access only the parent member.  
  6.     p.childmethod();//invalid cannot access child member   

main method 

Note: a reference of the parent class created using an object of the child class, if required, can be converted back into child class references again but there is no need to do an explicit conversion.
  1. static void Main(string[] args)  
  2. {  
  3.    //   
  4.    child obj = new child ();  
  5.    Parent p=obj;  

Converting parent references back to child references:

  1. child obj=(child )p;  
  2. child obj=p as child ; 

Finally the main block of the child looks like:

  1. static void Main(string[] args)  
  2. {      
  3.     child obj = new child ();  
  4.     Parent p=obj;              
  5.     obj.childmethod ();  
  6.     obj.ParentMethod ();  
  7.     Console.ReadKey();  

Rule 4: Each and every class we define in .Net languages has an implicit parent class (in other words, the class object) defined in the system namespace.

So the members of this class as in the following:

  • Equals
  • GetHashCode
  • GetType
  • ToString

Can be accessed or consumed anywhere.

Up Next
    Ebook Download
    View all
    Learn
    View all