Simple Program of Inheritance in Console in ASP.NET using C#

Inheritance: The most important reason to use OOP is to make reusability of Code and eliminate the redundant code. That can be done by one of the OOP concept i.e - -> INHERITANCE. Inheritance supports reusability by defining a class and then use that class again and again.

Inheritance supports a class hierarchy where Base class and Derived class exist, Suppose - -> Animals is the [Base Class] and Dog is the [Derived Class].Derived Class inherits the base class. Also Derived Class can inherits all the member of Base Class.

Let’s Begin our Program!.

Step 1: Open your Visual Studio. By pressing Ctrl +Shift + N you will get your “New Project” Window.

 
 
Figure 1: Create Console Application 

Step 2: After pressing OK you will get into your Coding Part where you will see three files in Solution Explorer [Properties, References, Program.cs], in which Program.cs file is your main file where you embed all your Inheritance program code.

 
 
Figure 2: Solution Explorer 

This is your Code for Inheritance Program:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Inheritance_demo21  
  6. {  
  7.     public class Baseclass {  
  8.         public int datamember;  
  9.         public void baseclassmethod()  
  10.         {  
  11.             Console.WriteLine("This is baseclass method");  
  12.             Console.WriteLine("-----------------------------------");          
  13.         }  
  14.     }  
  15.     public class DerivedClass : Baseclass  
  16.     {  
  17.         public void derivedclassmethod()  
  18.         {  
  19.             Console.WriteLine("This is our derivedclassmethod");  
  20.         }     
  21.     }  
  22.     class Program  
  23.     {  
  24.         static void Main(string[] args)  
  25.         {  
  26.             //create base class object  
  27.             Baseclass bc = new Baseclass();  
  28.             bc.datamember=1;  
  29.             bc.baseclassmethod();  
  30.   
  31.             //create derived class object  
  32.   
  33.             DerivedClass dc = new DerivedClass();  
  34.             dc.datamember = 2;  
  35.             dc.derivedclassmethod();  
  36.             dc.baseclassmethod();  
  37.             Console.ReadKey();  
  38.   
  39.         }  
  40.     }  
  41. }  

Output

Press F5 to get your output, you will get something like this :

 

Figure 3: Output 

Hope you like it! Have a nice day. Thank you for Reading!