Chapter 6: Object-Oriented Programming – Inheritance and Polymorphism
In this chapter, we continue our journey into Object-Oriented Programming (OOP) by exploring two of its most powerful concepts: Inheritance and Polymorphism.
These features allow developers to build scalable, reusable, and flexible applications by reusing code and enabling objects to behave differently based on context.
🎯 What You Will Learn
By the end of this chapter, you will:
- Understand the concept of inheritance in C#
- Learn how to create base and derived classes
- Explore method overriding and the
virtual/overridekeywords - Understand polymorphism and its types
- Use abstract classes and interfaces for design flexibility
- Practice hands-on exercises to reinforce learning
🔹 Inheritance
What is Inheritance?
- Inheritance allows a class (derived class) to reuse the properties and methods of another class (base class).
- Promotes code reusability and hierarchical relationships.
Syntax:
class BaseClass
{
public void ShowMessage()
{
Console.WriteLine("Message from BaseClass");
}
}
class DerivedClass : BaseClass
{
public void ShowDerivedMessage()
{
Console.WriteLine("Message from DerivedClass");
}
}
class Program
{
static void Main()
{
DerivedClass obj = new DerivedClass();
obj.ShowMessage(); // Inherited from BaseClass
obj.ShowDerivedMessage(); // Defined in DerivedClass
}
}
👉 Exercise:
- Create a
Vehiclebase class with a methodStart(). - Create a
Carderived class with a methodDrive(). - Demonstrate calling both methods from a
Carobject.
🔹 Method Overriding
Virtual and Override
- Use
virtualin the base class to allow overriding. - Use
overridein the derived class to change behavior.
Example:
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
Animal a = new Animal();
a.Speak(); // Animal makes a sound
Dog d = new Dog();
d.Speak(); // Dog barks
}
}
👉 Exercise:
- Create a
Shapebase class with a virtual methodDraw(). - Override it in
CircleandRectangleclasses to print different messages.
🔹 Polymorphism
What is Polymorphism?
- Polymorphism means "many forms."
- Allows objects of different types to be treated as objects of a common base type.
- Achieved through method overriding and interfaces.
Example:
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}
class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
}
class Program
{
static void Main()
{
Shape[] shapes = { new Circle(), new Rectangle() };
foreach (var shape in shapes)
{
shape.Draw(); // Calls appropriate method
}
}
}
👉 Exercise:
- Create a
Paymentbase class with a virtual methodProcessPayment(). - Override it in
CreditCardPaymentandPayPalPaymentclasses. - Demonstrate polymorphism by storing them in a
Payment[]array and callingProcessPayment().
🔹 Abstract Classes
What is an Abstract Class?
- A class that cannot be instantiated.
- May contain abstract methods (no implementation).
- Derived classes must implement abstract methods.
Example:
abstract class Animal
{
public abstract void Speak();
}
class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("Cat meows");
}
}
class Program
{
static void Main()
{
Animal a = new Cat();
a.Speak(); // Cat meows
}
}
👉 Exercise:
- Create an abstract class
Employeewith an abstract methodCalculateSalary(). - Implement it in
FullTimeEmployeeandPartTimeEmployeeclasses.
🔹 Interfaces
What is an Interface?
- Defines a contract with methods/properties but no implementation.
- Classes that implement the interface must provide implementation.
- Supports multiple inheritance (unlike classes).
Example:
interface IPrintable
{
void Print();
}
class Report : IPrintable
{
public void Print()
{
Console.WriteLine("Printing report...");
}
}
class Program
{
static void Main()
{
IPrintable printable = new Report();
printable.Print();
}
}
👉 Exercise:
- Create an interface
IDriveablewith methodDrive(). - Implement it in
CarandBikeclasses. - Demonstrate polymorphism by calling
Drive()on both.
📘 Chapter Summary
In this chapter, you learned:
- How to use inheritance to reuse code
- How to override methods with
virtualandoverride - How polymorphism enables flexible behavior
- How to design with abstract classes and interfaces
👉 You also practiced exercises to reinforce learning.