OOP Concepts in C#
Introduction
In this lesson, you will learn OOP concepts in C# for beginners with clear explanations and examples. Object-Oriented Programming helps you build clean, reusable, and scalable applications.
What is OOP?
OOP (Object-Oriented Programming) is a way of writing programs using classes and objects. It models real-world entities like Car, Student, or BankAccount.
1. Class and Object
A class is a blueprint. An object is an instance of that class.
class Car
{
public string color = "Red";
}
class Program
{
static void Main()
{
Car car1 = new Car();
Console.WriteLine(car1.color);
}
}
Explanation
- Class defines properties/behavior
- Object uses the class to access data
2. Encapsulation (Data Hiding)
Encapsulation means protecting data and exposing it using methods or properties.
class Person
{
private string name;
public void SetName(string n)
{
name = n;
}
public string GetName()
{
return name;
}
}
Why use it?
- Protect data
- Control access
- Improve security
3. Inheritance (Code Reuse)
Inheritance allows one class to use properties and methods of another class.
class Animal
{
public void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
}
Benefits
- Code reuse
- Cleaner structure
- Less duplication
4. Polymorphism (Many Forms)
Polymorphism allows the same method to behave differently.
Method Overloading
class Math
{
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
Method Overriding
class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal sound");
}
}
class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog barks");
}
}
5. Abstraction (Hide Complexity)
Abstraction hides complex implementation and shows only essential features.
abstract class Vehicle
{
public abstract void Start();
}
class Car : Vehicle
{
public override void Start()
{
Console.WriteLine("Car starts");
}
}
Use case
- Show only required functionality
- Hide internal logic
Real-World Example
Think of a Bank Account system:
- Class: BankAccount
- Encapsulation: balance is private
- Inheritance: SavingsAccount, CurrentAccount
- Polymorphism: different interest calculation
Key Points
- Class and Object are the base of OOP
- Encapsulation protects data
- Inheritance enables reuse
- Polymorphism provides flexibility
- Abstraction hides complexity
Internal Links
- Course Page: .NET Course for Beginners



