---
description: In this C# tutorial, you will learn about the concepts of Inheritance and Polymorphism in C# with Program Examples and detailed explanation.
title: C# Inheritance and Polymorphism with Program Examples
image: https://www.guru99.com/images/c-sharp-inheritance-and-polymorphism.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Inheritance and polymorphism in C# are two core object-oriented principles. Inheritance lets a child class reuse the fields and methods of a parent class, while polymorphism allows one method name to take many forms.

* 🧬 **Inheritance:** A child class inherits its parent class fields and methods and can add its own.
* 🔒 **Protected members:** Marking parent fields protected lets a derived child class reuse them directly.
* 🧩 **Polymorphism:** One method name takes many forms, so a single call behaves differently by context.
* 🔀 **Method overloading:** Several methods share one name but differ in parameters, resolved at compile time.
* 🧱 **Code reuse:** Both principles cut duplicate code and keep related classes consistent.
* 🤖 **AI assistance:** GitHub Copilot scaffolds base and derived classes; ML.NET reuses class hierarchies as model input.

[ Read More ](javascript:void%280%29;) 

![C# Inheritance and Polymorphism](https://www.guru99.com/images/c-sharp-inheritance-and-polymorphism.png)

## What is Inheritance in C#?

**Inheritance** is an important concept of C#. Inheritance is a concept in which you define parent classes and child classes. The child classes inherit methods and properties of the parent class, but at the same time, they can also modify the behavior of the methods if required. The child class can also define methods of its own if required.

Let’s get a better understanding of C# Inheritance by a Program Example:

## C# Inheritance Example

Let’s now see how we can incorporate the concept of inheritance in our code.

**Step 1)** The first step is to change the code for our Tutorial class. In this step, we add the below code to the Tutorial.cs file.

[![C# Inheritance Example](https://www.guru99.com/images/c-sharp-net/052616_1050_CClassandOb16.png)](https://www.guru99.com/images/c-sharp-net/052616%5F1050%5FCClassandOb16.png)

Note that we need to now add the access modifier of ‘protected’ to both the TutorialID and TutorialName field.

Remember we had mentioned this access modifier in the [Access Modifier tutorial](https://www.guru99.com/c-sharp-access-modifiers-constructor.html). Well here you can see the purpose of having this. Only when you have this access modifier (protected), the child class be able to use the fields of the parent class.

**Step 2)** The second step is to add our new child class. The name of this class will be “Guru99Tutorial”. In this step, we add the below code to the Tutorial.cs file. The code should be placed after the Tutorial class definition.

[![C# Inheritance Example](https://www.guru99.com/images/c-sharp-net/052616_1050_CClassandOb17.png)](https://www.guru99.com/images/c-sharp-net/052616%5F1050%5FCClassandOb17.png)

**Code Explanation:-**

1. The first step is to create the Guru99Tutorial child class. We also need to mention that this class is going to be a child class of the Tutorial class. This is done by the ‘:’ keyword.
2. Next, we are defining a method called RenameTutorial. It will be used to rename the TutorialName field. This method accepts a string variable which contains the new name of the Tutorial.
3. We then assigned the parameter pNewName to the TutorialName field. **Note**: – Even though we have not defined the TutorialName field in the “Guru99Tutorial” class, we are still able to access this field. This is because of the fact that “Guru99Tutorial” is a child class of Tutorial class. And because we made the fields of the Tutorial class as protected, they can be accessed by this class.

**Step 3)** The last step is to modify our main Program.cs file. In our console application, we are going to make an object of the Guru99Tutorial class. With this object, we are going to call the RenameTutorial method. We are then going to display the TutorialName field with the help of the GetTutorial method.

[![C# Inheritance Example](https://www.guru99.com/images/c-sharp-net/052616_1050_CClassandOb18.png)](https://www.guru99.com/images/c-sharp-net/052616%5F1050%5FCClassandOb18.png)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 public class Tutorial
 {
  protected int TutorialID; 
  protected string TutorialName;
  
  public void SetTutorial(int pID,string pName) 
  {
   TutorialID=pID;
   TutorialName=pName;
  }
  
  public String GetTutorial()
  {
   return TutorialName;
  }
 }
  public class Guru99Tutorial:Tutorial
  {
   public void RenameTutorial(String pNewName)
   {
    TutorialName=pNewName;
   }
  
  static void Main(string[] args) 
  {
   Guru99Tutorial pTutor=new Guru99Tutorial();
   
   pTutor.RenameTutorial(".Net by Guru99");
    
   Console.WriteLine(pTutor.GetTutorial());
    
   Console.ReadKey(); 
  }
 }
}

**Code Explanation:-**

1. The first step is to create an object for the Guru99Tutorial class. This is done via the ‘new’ keyword. Note that this time we are not creating an object of the Tutorial class.
2. We use the RenameTutorial method of the Guru99Tutorial class to change the TutorialName field. We pass the string “.Net by Guru99” to the RenameTutorial method.
3. We then call the GetTutorial method. Note that even though this method is not defined in the Guru99Tutorial class, we are still able to access this method. The output of the GetTutorial method is then displayed to the console via the Console.WriteLine method.

If the above code is entered properly and the program is executed successfully, the following output will be displayed.

**Output:**

[![C# Inheritance Example](https://www.guru99.com/images/c-sharp-net/052616_1050_CClassandOb19.jpg)](https://www.guru99.com/images/c-sharp-net/052616%5F1050%5FCClassandOb19.jpg)

From the output, we can clearly see that the TutorialName field was renamed to “.Net by Guru99”. This was made possible of the RenameTutorial method called by the child class.

## What is Polymorphism in C#?

**Polymorphism** in [C#](https://www.guru99.com/c-sharp-tutorial.html) is an OOPs concept where one name can have many forms. For example, you have a smartphone for communication. The communication mode you choose could be anything. It can be a call, a text message, a picture message, mail, etc. So, the goal is common, that is, communication, but their approach is different. This is called Polymorphism.

You will get a better understanding of C# Polymorphism by the below Program Example:

## C# Polymorphism Example

Let’s now see, how we can incorporate the concept of Polymorphism in our code.

**Step 1)** The first step is to change the code for our Tutorial class. In this step, we add the below code to the Tutorial.cs file.

[![C# Polymorphism Example](https://www.guru99.com/images/c-sharp-net/052616_1050_CClassandOb20.png)](https://www.guru99.com/images/c-sharp-net/052616%5F1050%5FCClassandOb20.png)

**Code Explanation:-**

1 & 2) The first step is the same as in our earlier examples. We are keeping the definition of the SetTutorial method as it is.

3) This method sets the TutorialID and the TutorialName based on the parameters pID and pName.

4) This is where we make a change to our class wherein we add a new method with the same name of SetTutorial. Only this time we are only passing one parameter which is the pName. In this method, we are just setting the field of TutorialName to pName.

**Step 2)** The last step is to modify our main Program.cs file. In our console application, we are going to make an object of the Guru99Tutorial class.

[![C# Polymorphism Example](https://www.guru99.com/images/c-sharp-net/052616_1050_CClassandOb21.png)](https://www.guru99.com/images/c-sharp-net/052616%5F1050%5FCClassandOb21.png)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Tutorial
 {
  public int TutorialID; 
  public string TutorialName;
  
  public void SetTutorial(int pID,string pName) 
  {
   TutorialID=pID;
   TutorialName=pName;
  }
  public void SetTutorial(string pName) 
  {
   TutorialName=pName;
  }
  public String GetTutorial()
  {
   return TutorialName;
  }
  
  static void Main(string[] args) 
  {
   Tutorial pTutor=new Tutorial();
   
   pTutor.SetTutorial(1,"First Tutorial");
   Console.WriteLine(pTutor.GetTutorial());
   
   pTutor.SetTutorial("Second Tutorial");
   Console.WriteLine(pTutor.GetTutorial());
    
   Console.ReadKey(); 
  }
 }
}

**Code Explanation:-**

1. In the first step, we are using the SetTutorial method with 2 parameters. Where we are passing both the TutorialID and TutorialName to this method.
2. In the second step, we are now calling the SetTutorial method with just one parameter. We are just passing the TutorialName to this method.

If the above code is entered properly and the program is run the following output will be displayed. If in case you wanted to also fetch the Tutorial ID along with the Tutorial Name , you should follow the below step

1. Create a separate method called public int GetTutorialID
2. In that method write the code line “return TutorialID.” This can be used to return the TutorialID to the calling program.

**Output:**

[![C# Polymorphism Example](https://www.guru99.com/images/c-sharp-net/052616_1050_CClassandOb22.png)](https://www.guru99.com/images/c-sharp-net/052616%5F1050%5FCClassandOb22.png)

From the output, we can clearly see that both methods were called successfully. Because of this, the strings “First Tutorial” and “Second Tutorial” were sent to the console.

## FAQs

🧬 What are the types of inheritance in C#?

C# supports single, multilevel, and hierarchical inheritance through classes. Multiple inheritance comes only through interfaces.

🚫 Does C# support multiple inheritance?

No. A C# class inherits from one base class only. Multiple inheritance uses several interfaces instead.

🔀 What is the difference between method overloading and overriding in C#?

Overloading uses one name with different parameters at compile time. Overriding redefines a virtual base method at run time.

🧩 What do the virtual and override keywords do in C#?

The virtual keyword makes a base method overridable; the override keyword redefines it in a derived class.

🔑 What is the base keyword used for in C#?

The base keyword lets a derived class call the parent constructor or reach hidden or overridden base members.

🔒 How do you stop a class from being inherited in C#?

Mark the class sealed. A sealed class cannot be a base class, preventing further inheritance and improving performance.

🤖 Can GitHub Copilot generate C# inheritance and polymorphism code?

Yes. GitHub Copilot scaffolds base and child classes and adds virtual and override methods from a comment or class name.

🧠 How do inheritance and polymorphism help in ML.NET machine learning?

ML.NET defines data and prediction classes that inherit shared members, and its pipeline reuses a common transformer base type.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter](https://www.guru99.com/images/footer-email-avatar-imges-1.png) Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/c-sharp-inheritance-and-polymorphism.png","url":"https://www.guru99.com/images/c-sharp-inheritance-and-polymorphism.png","width":"700","height":"250","caption":"C# Inheritance &amp; Polymorphism","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/c","name":"C#"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html","name":"C# Inheritance and Polymorphism with Program Examples"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html#webpage","url":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html","name":"C# Inheritance and Polymorphism with Program Examples","dateModified":"2026-07-11T15:13:23+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/c-sharp-inheritance-and-polymorphism.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker","description":"I'm Benjamin Walker, an expert in C, C++, and C# programming, providing resources to enhance your coding proficiency and project outcomes.","url":"https://www.guru99.com/author/benjamin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/benjamin-walker-author.png","url":"https://www.guru99.com/images/benjamin-walker-author.png","caption":"Benjamin Walker","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"C#","headline":"C# Inheritance and Polymorphism with Program Examples","description":"In this C# tutorial, you will learn about the concepts of Inheritance and Polymorphism in C# with Program Examples and detailed explanation.","keywords":"c#","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker"},"dateModified":"2026-07-11T15:13:23+05:30","image":{"@id":"https://www.guru99.com/images/c-sharp-inheritance-and-polymorphism.png"},"copyrightYear":"2026","name":"C# Inheritance and Polymorphism with Program Examples","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are the types of inheritance in C#?","acceptedAnswer":{"@type":"Answer","text":"C# supports single, multilevel, and hierarchical inheritance through classes. Multiple inheritance comes only through interfaces."}},{"@type":"Question","name":"Does C# support multiple inheritance?","acceptedAnswer":{"@type":"Answer","text":"No. A C# class inherits from one base class only. Multiple inheritance uses several interfaces instead."}},{"@type":"Question","name":"What is the difference between method overloading and overriding in C#?","acceptedAnswer":{"@type":"Answer","text":"Overloading uses one name with different parameters at compile time. Overriding redefines a virtual base method at run time."}},{"@type":"Question","name":"What do the virtual and override keywords do in C#?","acceptedAnswer":{"@type":"Answer","text":"The virtual keyword makes a base method overridable; the override keyword redefines it in a derived class."}},{"@type":"Question","name":"What is the base keyword used for in C#?","acceptedAnswer":{"@type":"Answer","text":"The base keyword lets a derived class call the parent constructor or reach hidden or overridden base members."}},{"@type":"Question","name":"How do you stop a class from being inherited in C#?","acceptedAnswer":{"@type":"Answer","text":"Mark the class sealed. A sealed class cannot be a base class, preventing further inheritance and improving performance."}},{"@type":"Question","name":"Can GitHub Copilot generate C# inheritance and polymorphism code?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot scaffolds base and child classes and adds virtual and override methods from a comment or class name."}},{"@type":"Question","name":"How do inheritance and polymorphism help in ML.NET machine learning?","acceptedAnswer":{"@type":"Answer","text":"ML.NET defines data and prediction classes that inherit shared members, and its pipeline reuses a common transformer base type."}}]}],"@id":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html#schema-1141472","isPartOf":{"@id":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/c-sharp-inheritance-polymorphism.html#webpage"}}]}
```
