Object-Oriented Programming (OOP) in C# - Textnotes

Object-Oriented Programming (OOP) in C#


C# is an object-oriented language. OOP helps structure programs using objects, classes, and relationships.

1. Classes & Objects

A class is a blueprint; an object is an instance of a class.


using System;

class Car
{
public string Brand;
public string Model;

public void DisplayInfo()
{
Console.WriteLine($"Brand: {Brand}, Model: {Model}");
}
}

class Program
{
static void Main()
{
Car car1 = new Car(); // Create object
car1.Brand = "Toyota";
car1.Model = "Corolla";
car1.DisplayInfo();
}
}

Output:


Brand: Toyota, Model: Corolla

2. Constructors

Constructors initialize objects.

2.1 Default Constructor


class Person
{
public string Name;

public Person() // Default constructor
{
Name = "Unknown";
}
}

2.2 Parameterized Constructor


class Person
{
public string Name;

public Person(string name) // Parameterized constructor
{
Name = name;
}
}

2.3 Static Constructor


class Demo
{
static Demo()
{
Console.WriteLine("Static constructor called once before any object");
}
}

3. Fields & Properties

  1. Fields → Variables declared inside a class.
  2. Properties → Encapsulate fields with getter/setter.

class Student
{
private int age; // Field

public int Age // Property
{
get { return age; }
set { age = value; }
}
}

4. Encapsulation (Getters & Setters)

Encapsulation protects data and allows controlled access.


class BankAccount
{
private double balance;

public double Balance
{
get { return balance; }
set
{
if (value >= 0)
balance = value;
else
Console.WriteLine("Invalid balance");
}
}
}

5. Inheritance

Inheritance allows a class to reuse members of another class.

5.1 Single Inheritance


class Animal
{
public void Eat() => Console.WriteLine("Eating...");
}

class Dog : Animal
{
public void Bark() => Console.WriteLine("Barking...");
}

5.2 Multilevel Inheritance


class Vehicle
{
public void Start() => Console.WriteLine("Vehicle started");
}

class Car : Vehicle
{
public void Drive() => Console.WriteLine("Car is driving");
}

class SportsCar : Car
{
public void Turbo() => Console.WriteLine("Turbo activated");
}

6. Polymorphism

Polymorphism allows same interface, different behavior.

6.1 Method Overloading (Compile-time)


class Calculator
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}

6.2 Method Overriding (Run-time)


class Animal
{
public virtual void Sound() => Console.WriteLine("Animal sound");
}

class Dog : Animal
{
public override void Sound() => Console.WriteLine("Dog barks");
}

7. Abstraction (Abstract Classes)

Abstract classes cannot be instantiated and may have abstract methods.


abstract class Shape
{
public abstract void Draw(); // Abstract method
}

class Circle : Shape
{
public override void Draw() => Console.WriteLine("Drawing Circle");
}

8. Interfaces

Interfaces define a contract without implementation.


interface IFlyable
{
void Fly();
}

class Bird : IFlyable
{
public void Fly() => Console.WriteLine("Bird is flying");
}

9. Sealed Classes

Sealed classes cannot be inherited.


sealed class FinalClass
{
public void Display() => Console.WriteLine("Sealed Class Method");
}

// class Derived : FinalClass {} // ❌ Error

10. Static Classes & Members

  1. Static Class: Cannot create objects, contains only static members.
  2. Static Members: Belong to class, not instance.

static class MathHelper
{
public static int Square(int num) => num * num;
}

Console.WriteLine(MathHelper.Square(5)); // 25

11. this keyword

this refers to the current instance of the class.


class Employee
{
private string Name;

public Employee(string Name)
{
this.Name = Name; // Differentiates field & parameter
}

public void Display() => Console.WriteLine("Name: " + this.Name);
}

Summary of Chapter 5:

  1. Class & Object → Core building blocks of OOP.
  2. Constructors → Initialize objects.
  3. Fields & Properties → Encapsulate data.
  4. Inheritance → Reuse code.
  5. Polymorphism → Overloading & overriding for flexibility.
  6. Abstraction & Interfaces → Define contracts without implementation.
  7. Sealed & Static → Restrict inheritance or provide utility methods.
  8. this keyword → Refers to the current object instance.