Object-Oriented Programming (OOP) helps us design applications using real-world concepts.
Below we explore important OOP principles and relationships in C#, along with examples.
Class
A Class is a blueprint that defines properties and methods.
public class Car
{
public string Brand { get; set; }
public void Drive()
{
Console.WriteLine($"{Brand} is driving.");
}
}
Object
An Object is an instance of a class.
Car car1 = new Car { Brand = "Toyota" };
car1.Drive(); // Output: Toyota is driving.
Abstraction
Abstraction focuses on essential details while hiding the complexity.
public abstract class Payment
{
public abstract void ProcessPayment(decimal amount);
}
public class CreditCardPayment : Payment
{
public override void ProcessPayment(decimal amount)
{
Console.WriteLine($"Paid {amount} using Credit Card.");
}
}
Encapsulation
Encapsulation hides implementation details using access modifiers.
public class BankAccount
{
private decimal balance;
public void Deposit(decimal amount) => balance += amount;
public decimal GetBalance() => balance; // only controlled access
}
Polymorphism
Polymorphism allows the same method name to perform different tasks.
Overloading
public class Calculator
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}
Overriding
public class Animal
{
public virtual void Speak() => Console.WriteLine("Animal sound");
}
public class Dog : Animal
{
public override void Speak() => Console.WriteLine("Bark");
}
Inheritance
Inheritance lets a class reuse properties and methods from a parent class.
public class Vehicle
{
public void Start() => Console.WriteLine("Vehicle started");
}
public class Bike : Vehicle
{
public void RingBell() => Console.WriteLine("Bell rings!");
}
Sealed Class
A sealed class cannot be inherited.
public sealed class Logger
{
public void Log(string msg) => Console.WriteLine(msg);
}
Multiple Inheritance
C# does not allow multiple inheritance of classes, but interfaces provide it.
public interface IFly
{
void Fly();
}
public interface ISwim
{
void Swim();
}
public class Duck : IFly, ISwim
{
public void Fly() => Console.WriteLine("Duck flying");
public void Swim() => Console.WriteLine("Duck swimming");
}
Abstract Class
An abstract class cannot be instantiated directly; it must be inherited.
public abstract class Shape
{
public abstract double Area();
}
public class Circle : Shape
{
public double Radius { get; set; }
public override double Area() => Math.PI * Radius * Radius;
}
Generalization
Generalization groups common features into a parent class.
public class Teacher : Person { }
public class Student : Person { }
public class Person
{
public string Name { get; set; }
}
Association
Association represents a relationship between classes.
public class Customer
{
public string Name { get; set; }
}
public class Order
{
public Customer OrderedBy { get; set; }
}
Aggregation
Aggregation is a weak “whole-part” relationship.
public class Department
{
public List Employees { get; set; } = new List();
}
public class Employee
{
public string Name { get; set; }
}
Composition
Composition is a strong “whole-part” relationship. If the whole is destroyed, parts are also destroyed.
public class House
{
private Room room;
public House()
{
room = new Room(); // Room cannot exist without House
}
}
public class Room { }
Multiplicity
Multiplicity defines how many objects can participate in a relationship.
public class Library
{
public List Books { get; set; } = new List();
}
public class Book { }
Interface
An interface defines a contract without implementation.
public interface INotifier
{
void Send(string message);
}
public class EmailNotifier : INotifier
{
public void Send(string message)
{
Console.WriteLine($"Email sent: {message}");
}
}
Summary Table
| Concept | Definition | Example |
|---|---|---|
| Class | Blueprint of objects | Car |
| Object | Instance of a class | car1 = new Car() |
| Abstraction | Focus on essentials | Payment (abstract) |
| Encapsulation | Data hiding | BankAccount with private balance |
| Polymorphism | Many forms | Calculator.Add() overloads, Dog.Speak() override |
| Inheritance | Reuse parent properties | Bike inherits Vehicle |
| Sealed Class | Cannot be inherited | Logger |
| Multiple Inheritance | Via interfaces | Duck : IFly, ISwim |
| Abstract Class | Must be inherited | Shape |
| Generalization | Group common features | Person parent for Student, Teacher |
| Association | Relationship | Order → Customer |
| Aggregation | Weak whole-part | Department → Employees |
| Composition | Strong whole-part | House → Room |
| Multiplicity | How many objects | Library → Books |
| Interface | Contract without implementation | INotifier |