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.
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 |
4Build a Banking Example
Let's create one parent class and two child classes. Each account calculates its own interest.
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
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.
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
What is inheritance in C#?
Answer: Inheritance allows a derived class to reuse accessible members of a base class. It reduces duplicate code.
What is polymorphism?
Answer: Polymorphism means many forms. The same method call can execute different implementations based on the actual object.
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.
Does C# support multiple class inheritance?
Answer: No. A class can inherit from only one class. However, it can implement multiple interfaces.
What is runtime polymorphism?
Answer: It is method selection at runtime based on the actual object type. Overridden virtual methods support this behaviour.