LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Inheritance and Polymorphism in C#

Inheritance allows one class to reuse another class's properties and methods. Polymorphism means one method can behave differently depending on the object. These concepts help us build reusable and flexible applications. We use them in payment systems, banking applications, APIs and enterprise software.

1How Inheritance and Polymorphism Work

Hi friends, in this article we will learn inheritance and polymorphism in C#. Inheritance allows one class to reuse another class's properties and methods. Polymorphism means one method can behave differently depending on the object. These concepts help us build reusable and flexible applications. We use them in payment systems, banking applications, APIs and enterprise software.

2Think About a Bank Account

Imagine a bank offers savings and current accounts. Both accounts have a common account holder and balance. Instead of writing the same code twice, we create a parent class called BankAccount.

SavingsAccount and CurrentAccount inherit its common features. But each account can calculate interest or charges differently. This is where polymorphism helps.

Remember Inheritance shares common functionality. Polymorphism allows different implementations of the same method.

3Understand the Important Ideas

Inheritance

A child class gets accessible members from a parent class using the colon symbol (:).

Base and Derived Classes

The parent is called the base class. The child is called the derived class.

Virtual and Override

virtual allows a method to be changed in a child class. override provides that new implementation.

Polymorphism

A base class reference can point to a child object. Calling an overridden method executes the child's version.

Keyword Purpose
class Defines a class
: Inherits a base class
virtual Allows overriding
override Replaces inherited behaviour
Pro Tip Use inheritance when classes share a genuine relationship and common behaviour.
Common Mistake A method cannot be overridden unless the base method is virtual, abstract or already overrideable.

4Build a Banking Example

Let's create one parent class and two child classes. Each account calculates its own interest.

Program.csC#
using System;

class BankAccount
{
    public string Holder = "Rahul";
    public virtual void CalculateInterest()
    {
        Console.WriteLine("Standard interest");
    }
}

class SavingsAccount : BankAccount
{
    public override void CalculateInterest()
    {
        Console.WriteLine("Savings interest: 5%");
    }
}

class CurrentAccount : BankAccount
{
    public override void CalculateInterest()
    {
        Console.WriteLine("Current account: No interest");
    }
}

class Program
{
    static void Main()
    {
        BankAccount account;

        account = new SavingsAccount();
        Console.WriteLine(account.Holder);
        account.CalculateInterest();

        account = new CurrentAccount();
        account.CalculateInterest();
    }
}

Notice that both child classes inherit Holder. The same base reference calls different implementations of CalculateInterest.

5Check the Program Result

Output
Rahul
Savings interest: 5%
Current account: No interest

The first object is SavingsAccount, so its method runs. The second object is CurrentAccount, so its method runs instead.

6Watch the Code Execute

See how the base reference points to different account objects and invokes the correct overridden method.

Program.cs - Visual Studio Code
▤⌕⑂▦
● Program.cs
 
PROBLEMS  OUTPUT  DEBUG CONSOLE  TERMINAL
 
ReadyUTF-8  CRLF  C#

7What Should You Remember?

Inheritance helps reuse common code. Polymorphism makes applications flexible. Virtual and override methods allow child classes to provide their own behaviour.

  • Use : to inherit a class.
  • Use virtual and override for runtime polymorphism.
  • Use a base reference to work with different child objects.

8Questions Interviewers May Ask

Q1Easy

What is inheritance in C#?

Answer: Inheritance allows a derived class to reuse accessible members of a base class. It reduces duplicate code.

Q2Easy

What is polymorphism?

Answer: Polymorphism means many forms. The same method call can execute different implementations based on the actual object.

Q3Medium

What is the difference between virtual and override?

Answer: Virtual marks a base method as replaceable. Override provides the replacement implementation in a derived class.

Q4Medium

Does C# support multiple class inheritance?

Answer: No. A class can inherit from only one class. However, it can implement multiple interfaces.

Q5Hard

What is runtime polymorphism?

Answer: It is method selection at runtime based on the actual object type. Overridden virtual methods support this behaviour.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.