Object-Oriented Programming in C# – Classes, Interfaces, and Abstract Classes Explained

Object-Oriented Programming in C# – Classes, Interfaces, and Abstract Classes Explained

Object-Oriented Programming (OOP) is the core of C#. Almost every real-world C# application is built using OOP principles. In this guide, you will learn all major OOP concepts in C# with clear explanations and examples.

What is Object-Oriented Programming?

OOP is a programming approach where programs are built using objects that represent real-world entities. Each object contains data (fields) and behavior (methods).

1. Class and Object in C#

A class is a blueprint, and an object is an instance of that class.

class Car {
    public string Brand;
    public int Speed;

    public void Show() {
        Console.WriteLine(Brand + " " + Speed);
    }
}

Car c1 = new Car();
c1.Brand = "Tesla";
c1.Speed = 200;
c1.Show();

2. Encapsulation

Encapsulation means hiding data and providing controlled access using properties.

class Student {
    private int marks;

    public int Marks {
        get { return marks; }
        set { marks = value; }
    }
}

3. Inheritance

Inheritance allows one class to use the properties and methods of another class.

class Animal {
    public void Eat() {
        Console.WriteLine("Animal eats");
    }
}

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

4. Polymorphism

Polymorphism allows methods to behave differently based on the object.

class Shape {
    public virtual void Draw() {
        Console.WriteLine("Drawing Shape");
    }
}

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

5. Abstract Class

An abstract class cannot be instantiated and can contain abstract methods.

abstract class Vehicle {
    public abstract void Start();
}

6. Interface in C#

An interface defines a contract that a class must follow.

interface IPrintable {
    void Print();
}

class Report : IPrintable {
    public void Print() {
        Console.WriteLine("Printing Report");
    }
}

Difference Between Abstract Class and Interface

  • Abstract class can have implementation, interface cannot (before C# 8)
  • A class can inherit only one abstract class but multiple interfaces

Common OOP Interview Questions in C#

  • Difference between abstract class and interface
  • What is virtual and override?
  • What is encapsulation?
  • Explain runtime polymorphism

Conclusion

OOP is the backbone of C# programming. Understanding classes, inheritance, interfaces, and abstraction will help you write clean, scalable, and professional applications.

Next Post: Top C# Interview Questions and Answers for Freshers

Comments

Popular posts from this blog

Top 10 Free Coding Websites Every Beginner Should Use in 2026

Graph Data Structure – Complete Beginner to Advanced Guide with BFS, DFS and Examples

5 JavaScript Console Methods You're Not Using (But Should Be)