Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Master C# Inheritance & Polymorphism: Practical Code Examples

What Is Inheritance in C#?

Inheritance is a cornerstone of C# object‑oriented design. It lets a child class automatically receive the fields and methods of a parent class, while still permitting the child to override or extend that behavior. This promotes code reuse, simplifies maintenance, and establishes a clear hierarchy.

C# Inheritance Example

Below is a step‑by‑step demonstration that shows how to implement inheritance and use protected members.

Step 1 – Modify the Base Class

Open Tutorial.cs and change the Tutorial class so its key fields are protected:

Master C# Inheritance & Polymorphism: Practical Code Examples

Using protected ensures that only derived classes can access these fields directly.

Step 2 – Create a Derived Class

Append the following code to Tutorial.cs after the base class definition:

Master C# Inheritance & Polymorphism: Practical Code Examples

Explanation:

  1. Declare Guru99Tutorial as a child of Tutorial using the colon (:) syntax.
  2. Add a method RenameTutorial that assigns a new value to TutorialName.
  3. Because TutorialName is protected, Guru99Tutorial can access it even though it isn’t explicitly declared there.

Step 3 – Use the Derived Class in Program.cs

Replace the contents of Program.cs with the following:

Master C# Inheritance & Polymorphism: Practical Code Examples

using System;
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();
        }
    }
}

Explanation:

  1. The program instantiates Guru99Tutorial, not the base Tutorial.
  2. It calls RenameTutorial to change the title.
  3. Even though GetTutorial is defined in the base class, the derived instance can invoke it directly.

Running this code prints:

Master C# Inheritance & Polymorphism: Practical Code Examples

What Is Polymorphism in C#?

Polymorphism lets a single interface represent different underlying forms. In C#, this often manifests as method overloading or overriding, allowing the same method name to perform varied tasks based on parameters or context.

C# Polymorphism Example

We’ll demonstrate method overloading within the same class.

Step 1 – Update the Base Class

Modify Tutorial.cs as shown:

Master C# Inheritance & Polymorphism: Practical Code Examples

Key changes:

  1. The SetTutorial method now has two overloads – one that accepts an int and string, and another that accepts only a string.
  2. Both overloads assign values to the corresponding fields.

Step 2 – Test in Program.cs

Replace Program.cs with the following code:

using System;
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();
        }
    }
}

Explanation:

  1. First call passes both id and name to the two‑parameter overload.
  2. Second call passes only name, invoking the single‑parameter overload.

The console displays:

Master C# Inheritance & Polymorphism: Practical Code Examples

For advanced scenarios, you can add a GetTutorialID method to retrieve the ID separately.

Summary

C Language

  1. C# Expressions, Statements & Blocks: A Comprehensive Guide with Practical Examples
  2. C++ Inheritance Models: Multiple, Multilevel, Hierarchical
  3. C++ Classes & Objects: A Practical Guide with Code Examples
  4. C++ Functions Explained with Practical Code Examples
  5. Master C# Classes & Objects: A Practical Tutorial with Real‑World Examples
  6. Understanding C# Access Modifiers (Specifiers) with Practical Examples
  7. Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained
  8. Understanding type() and isinstance() in Python: Practical Examples
  9. Mastering C# Inheritance: Build Reusable, Maintainable Code
  10. C# Polymorphism: Static vs Dynamic Binding Explained