C# – Object-Oriented Programming (OOP)
1. Object-Oriented Concepts
-
Class: Blueprint/template for creating objects.
-
Object: Instance of a class.
-
Encapsulation: Hiding data using access modifiers (public/private).
-
Abstraction: Hiding implementation details, showing only essentials.
-
Inheritance: One class derives from another, reusing code.
-
Polymorphism: Ability of a method/behavior to take many forms.
Example:
class Car // class
{
public string brand = "BMW"; // field
public void Drive() // method
{
Console.WriteLine("Car is driving");
}
}
class Program
{
static void Main()
{
Car c = new Car(); // object
Console.WriteLine(c.brand);
c.Drive();
}
}2. Working with Indexer and Properties
Properties
- Used to encapsulate fields with
getandsetmethods.
class Student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}Indexer
- Provides array-like access to class objects.
class Sample
{
private int[] arr = new int[5];
public int this[int index]
{
get { return arr[index]; }
set { arr[index] = value; }
}
}
class Program
{
static void Main()
{
Sample s = new Sample();
s[0] = 100; // using indexer
Console.WriteLine(s[0]);
}
}3. Constructor & Destructor
-
Constructor: Special method, same name as class, initializes objects.
-
Destructor: Cleans up resources, defined with
~ClassName().
Example:
class Demo
{
public Demo()
{
Console.WriteLine("Constructor called");
}
~Demo()
{
Console.WriteLine("Destructor called");
}
}4. Working with Static Members
- Static variable/method → Belongs to the class, not an instance.
Example:
class Counter
{
public static int count = 0;
public Counter() { count++; }
}
class Program
{
static void Main()
{
new Counter();
new Counter();
Console.WriteLine(Counter.count); // Output: 2
}
}5. Inheritance & Polymorphism
-
Inheritance: Reuse properties and methods of base class.
-
Polymorphism: Achieved by method overriding (runtime) and method overloading (compile-time).
Example:
class Animal
{
public virtual void Speak() { Console.WriteLine("Animal sound"); }
}
class Dog : Animal
{
public override void Speak() { Console.WriteLine("Bark"); }
}
class Program
{
static void Main()
{
Animal a = new Dog(); // polymorphism
a.Speak(); // Bark
}
}6. Types of Inheritance
-
Single → One base, one derived.
-
Multilevel → Chain of inheritance.
-
Hierarchical → One base, many derived.
-
Multiple → Supported using interfaces (not directly).
Example (Multilevel):
class A { }
class B : A { }
class C : B { }7. Constructor in Inheritance
- Base constructor runs before derived constructor.
Example:
class A
{
public A() { Console.WriteLine("Base Constructor"); }
}
class B : A
{
public B() { Console.WriteLine("Derived Constructor"); }
}
class Program
{
static void Main() { B obj = new B(); }
}
// Output:
// Base Constructor
// Derived Constructor8. Interface Implementation
-
Interface: Contract with only declarations (no implementation).
-
A class must implement all members.
Example:
interface IShape
{
void Draw();
}
class Circle : IShape
{
public void Draw() { Console.WriteLine("Drawing Circle"); }
}9. Operator and Method Overloading, Overriding
Method Overloading
- Same method name, different parameters.
class MathOp
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}Method Overriding
- Redefining base class method with
override
class Base
{
public virtual void Show() { Console.WriteLine("Base show"); }
}
class Derived : Base
{
public override void Show() { Console.WriteLine("Derived show"); }
}Operator Overloading
- Redefine operators using
operatorkeyword.
class Complex
{
public int real, imag;
public Complex(int r, int i) { real = r; imag = i; }
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imag + c2.imag);
}
}10. Static and Dynamic Binding
-
Static Binding: Compile-time, method call decided at compile time. (method overloading)
-
Dynamic Binding: Runtime, method call decided at runtime. (method overriding with
virtual)
11. Virtual Methods
- Allow derived class to override behavior.
class Base
{
public virtual void Show() { Console.WriteLine("Base show"); }
}
class Derived : Base
{
public override void Show() { Console.WriteLine("Derived show"); }
}12. Abstract Class
- Cannot be instantiated, can have abstract (unimplemented) and normal methods.
abstract class Shape
{
public abstract void Draw(); // must be implemented
}
class Circle : Shape
{
public override void Draw() { Console.WriteLine("Drawing Circle"); }
}13. sealed Keyword
-
sealed class → cannot be inherited.
-
sealed method → cannot be overridden.
sealed class FinalClass { } // cannot inherit
class Base
{
public virtual void Display() { }
}
class Derived : Base
{
public sealed override void Display() { } // cannot override further
}✅ These are complete exam-style, pointwise OOP notes with code.
Do you want me to also make a summary comparison table (e.g., abstract vs interface, static vs instance, override vs overload) for quick revision?