Day 7 – Object-Oriented Programming in C# Explained with Examples

Format: Case study + tutorial + hierarchical diagrams

Introduction

Object-Oriented Programming (OOP) is the backbone of modern software development. In C#, OOP enables developers to create modular, reusable, and maintainable code. At CuriosityTech.in, our goal is to make OOP approachable, practical, and relevant for aspiring FullStack .NET developers.

Through this blog, you’ll learn OOP principles in C# with real-world examples, diagrams, and hands-on exercises.


1. Core Principles of OOP in C#

C# supports four primary OOP principles:

PrincipleDescriptionExample
EncapsulationHiding internal details and exposing only what’s necessaryUsing private fields with public getters/setters
InheritanceDeriving new classes from existing ones to reuse codeclass Car : Vehicle { }
PolymorphismObjects can take multiple forms (method overloading/overriding)virtual and override keywords
AbstractionExposing essential features without implementation detailsUsing abstract class or interface

2. Encapsulation in C#

Encapsulation protects data by restricting direct access.

class BankAccount

{

    private double balance;

    public void Deposit(double amount)

    {

        if(amount > 0) balance += amount;

    }

    public double GetBalance()

    {

        return balance;

    }

}

BankAccount account = new BankAccount();

account.Deposit(500);

Console.WriteLine(account.GetBalance()); // 500

Explanation:

  • The balance field is private, so it cannot be accessed directly.

  • Deposit() and GetBalance() methods control access safely.

Diagram:

[BankAccount Class]

   |– balance : double (private)

   |– Deposit(amount) : void

   |– GetBalance() : double


3. Inheritance

Inheritance promotes code reuse.

class Vehicle

{

    public string Brand;

    public void Start() => Console.WriteLine($”{Brand} started.”);

}

class Car : Vehicle

{

    public string Model;

}

Car myCar = new Car();

myCar.Brand = “BMW”;

myCar.Model = “X5”;

myCar.Start(); // BMW started.

Explanation:

  • Car inherits from Vehicle, so it gains the Start method and Brand property.

  • This reduces duplicate code across similar classes.

Hierarchical Diagram:

        Vehicle

         /     \

       Car     Bike


4. Polymorphism

Polymorphism allows objects to behave differently while sharing the same interface.

class Shape

{

    public virtual void Draw() => Console.WriteLine(“Drawing Shape”);

}

class Circle : Shape

{

    public override void Draw() => Console.WriteLine(“Drawing Circle”);

}

Shape shape = new Circle();

shape.Draw(); // Drawing Circle

Explanation:

  • Draw() is overridden in the Circle class, demonstrating runtime polymorphism.


5. Abstraction

Abstraction hides complexity.

abstract class Animal

{

    public abstract void MakeSound();

}

class Dog : Animal

{

    public override void MakeSound() => Console.WriteLine(“Bark!”);

}

Dog dog = new Dog();

dog.MakeSound(); // Bark!

Explanation:

  • Abstract classes cannot be instantiated.

  • Forces derived classes to implement essential behaviors.


6. Case Study: Online Learning Platform

Imagine CuriosityTech.in wants to model its courses, students, and instructors:

Classes & Relationships:

ClassPropertiesMethods
CourseName, Duration, InstructorEnrollStudent(), StartCourse()
StudentName, EmailRegister(), ViewCourse()
InstructorName, ExpertiseAddCourse(), GradeStudent()

Diagram:

         Person (abstract)

           /       \

      Student       Instructor

          |

       EnrolledCourses

           |

         Course

Real-world Application:

  • Encapsulation: Hide internal student grades.

  • Inheritance: Instructor & Student derive from Person.

  • Polymorphism: StartCourse() behaves differently for live or recorded classes.

  • Abstraction: Expose only course enrollment features to students.


7. Practical Tips from CuriosityTech.in

  • Hands-on exercises: Implement mini-projects like a library management system.

  • Mentorship: Step-by-step guidance on applying OOP principles.

  • Project-ready skills: Apply OOP to both backend and frontend integration.

8. Hierarchy diagram


Infographic diagram


Conclusion

Mastering OOP in C# is essential for building scalable, maintainable, and efficient .NET applications. With the combination of theoretical concepts, real-world examples, and mentorship from CuriosityTech.in, you’ll be ready to move to Day 8: ASP.NET Core MVC and start building dynamic web applications.


Leave a Comment

Your email address will not be published. Required fields are marked *