Aral Code

Object-Oriented Programming Lesson 12 of 15

C# Inheritance, Polymorphism and Interfaces

Build classes on others with inheritance and base, override virtual methods, use abstract classes and interfaces, and treat different objects the same way with polymorphism.

Inheritance lets one class reuse and extend another. Polymorphism lets code work with a base type while each object behaves in its own way. Interfaces describe abilities that unrelated classes can share.

Inheritance

var s = new Student("Ana", 10);
s.Introduce();
s.Study();

class Person
{
    public string Name { get; }
    public Person(string name) => Name = name;
    public void Introduce() => Console.WriteLine($"Hi, I'm {Name}.");
}

class Student : Person
{
    public int GradeLevel { get; }
    public Student(string name, int gradeLevel) : base(name)
    {
        GradeLevel = gradeLevel;
    }
    public void Study() => Console.WriteLine($"{Name} (Grade {GradeLevel}) is studying.");
}

Output

Hi, I'm Ana.
Ana (Grade 10) is studying.
  • class Student : Person means Student inherits from Person.
  • : base(name) calls the parent's constructor.
  • C# allows only one base class, but many interfaces.

virtual and override

var rides = new List<Vehicle> { new Jeepney(), new Tricycle(), new Taxi() };
foreach (Vehicle v in rides)
{
    Console.WriteLine($"{v.Type,-9}{v.Fare(6),8:N2}");
}

class Vehicle
{
    public virtual string Type => "Vehicle";
    public virtual decimal Fare(double km) => 0m;
}
class Jeepney : Vehicle
{
    public override string Type => "Jeepney";
    public override decimal Fare(double km) => 13m + (decimal)Math.Max(0, km - 4) * 1.80m;
}
class Tricycle : Vehicle
{
    public override string Type => "Tricycle";
    public override decimal Fare(double km) => 20m + (decimal)km * 5m;
}
class Taxi : Vehicle
{
    public override string Type => "Taxi";
    public override decimal Fare(double km) => 50m + (decimal)km * 13.5m;
}

Output

Jeepney     16.60
Tricycle    50.00
Taxi       131.00

The loop only knows it has Vehicle objects, yet each one computes its own fare. That's polymorphism. (These fare formulas are made-up examples.)

Abstract classes

Shape[] shapes = { new Circle(1), new Rectangle(3, 4) };
foreach (var s in shapes)
{
    Console.WriteLine($"{s.GetType().Name}: {s.Area():F2}");
}

abstract class Shape
{
    public abstract double Area();   // no body: subclasses must provide it
}
class Circle : Shape
{
    private readonly double r;
    public Circle(double r) => this.r = r;
    public override double Area() => Math.PI * r * r;
}
class Rectangle : Shape
{
    private readonly double w, h;
    public Rectangle(double w, double h) { this.w = w; this.h = h; }
    public override double Area() => w * h;
}

Output

Circle: 3.14
Rectangle: 12.00

You can't write new Shape(); abstract classes exist only to be inherited.

Interfaces

An interface lists members a class promises to have. By convention, interface names start with I:

var bills = new List<IPayable> { new Tuition(15000m), new CanteenBill(20) };
decimal total = 0;
foreach (var b in bills)
{
    Console.WriteLine($"{b.Description}: {b.AmountDue:N2}");
    total += b.AmountDue;
}
Console.WriteLine($"Total due: {total:N2}");

interface IPayable
{
    string Description { get; }
    decimal AmountDue { get; }
}
class Tuition : IPayable
{
    public Tuition(decimal amount) => AmountDue = amount;
    public string Description => "Tuition";
    public decimal AmountDue { get; }
}
class CanteenBill : IPayable
{
    private readonly int meals;
    public CanteenBill(int meals) => this.meals = meals;
    public string Description => $"Canteen ({meals} meals)";
    public decimal AmountDue => meals * 45m;
}

Output

Tuition: 15,000.00
Canteen (20 meals): 900.00
Total due: 15,900.00

The four pillars of OOP

PillarC# tools
Encapsulationprivate fields, properties with validation
Inheritanceclass B : A, base(...)
Polymorphismvirtual / override, interfaces
Abstractionabstract classes, interface

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How does a derived class call the base constructor?
2. What must a base method be marked to allow override?
3. How many interfaces can a C# class implement?
4. By convention, interface names start with…

By Sir Reyel · Last updated 2026-12-04