OOP (Object-Oriented Programming) is a programming paradigm based on objects that encapsulate data (fields/properties) and behavior (methods). It helps structure code to be more modular, reusable, and maintainable. C# is a fully object-oriented language supporting all core OOP principles.
1. Class and Object
Class: A blueprint/template for creating objects. It defines properties, methods, and events.
Object: An instance of a class.
class Car
{
public string Model;
public void Start() { Console.WriteLine("Car started"); }
}
Car myCar = new Car(); // object creation
myCar.Model = "BMW";
myCar.Start();
2. Encapsulation
Definition: Wrapping data and methods inside a class and restricting access to internal details.
Access modifiers:private, public, protected, etc.
class Person
{
private string name; // hidden
public string Name // controlled access
{
get { return name; }
set { name = value; }
}
}
Provides data security and controlled access.
3. Inheritance
Definition: Enables a class to inherit properties and methods from another class.
class Animal
{
public void Eat() { Console.WriteLine("Eating"); }
}
class Dog : Animal
{
public void Bark() { Console.WriteLine("Barking"); }
}
Dog can use Eat() from Animal.
Types of Inheritance in C#:
Single Inheritance: One class inherits from one class.
Multilevel Inheritance: Class hierarchy, e.g., A -> B -> C.
Hierarchical Inheritance: Multiple classes inherit from one base class.
Note: C# does not support multiple class inheritance directly but can implement multiple interfaces.
4. Polymorphism
Definition: “Many forms” — the ability to perform the same action in different ways.
a. Compile-Time Polymorphism (Method Overloading)
class Calculator
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}
b. Run-Time Polymorphism (Method Overriding)
class Animal
{
public virtual void Speak() { Console.WriteLine("Animal sound"); }
}
class Dog : Animal
{
public override void Speak() { Console.WriteLine("Bark"); }
}
5. Abstraction
Definition: Hiding complex details and showing only the essential features.
Achieved using abstract classes and interfaces.
abstract class Shape
{
public abstract double Area();
}
class Circle : Shape
{
private double radius;
public Circle(double r) { radius = r; }
public override double Area() => Math.PI * radius * radius;
}
6. Interfaces
Define a contract that classes must implement.
interface IVehicle
{
void Drive();
}
class Car : IVehicle
{
public void Drive() { Console.WriteLine("Car is driving"); }
}
7. Constructors and Destructors
Constructor: Special method called when an object is created.
Destructor: Called when an object is destroyed (rarely used in C# because of garbage collection).
class Car
{
public string Model;
public Car(string model) { Model = model; }
}
A class can have objects of other classes to reuse functionality.
class Engine
{
public void Start() { Console.WriteLine("Engine started"); }
}
class Car
{
Engine engine = new Engine();
public void StartCar() { engine.Start(); }
}
Key Advantages of OOP
Reusability: Inheritance allows code reuse.
Encapsulation: Protects data and implementation.
Flexibility: Polymorphism allows different implementations.
Maintainability: Easy to modify and extend.
Modularity: Classes and objects divide code logically.
Object Oriented Programming is not a system, it is a paradigm or model. The term system implies something specific, such as a specific application or other software. People use the acronym OOPS and then other people try to give the acronym meaning but the correct acronym is OOP.
OOPs (Object-Oriented Programming System) concepts are the fundamental principles of object-oriented programming, mainly used in languages like C#, Java, Python, C++ etc.
Here are the main OOPs concepts with simple explanation and examples:
1. Class
A class is a blueprint or template for creating objects.
It defines properties (variables/fields) and behaviors (methods/functions).
Example (C#):
public class Car
{
public string Brand;
public int Speed;
public void Drive()
{
Console.WriteLine(Brand + " is driving at " + Speed + " km/h");
}
}
2. Object
An object is an instance of a class.
It represents a real-world entity.
Example:
Car myCar = new Car();
myCar.Brand = "BMW";
myCar.Speed = 120;
myCar.Drive(); // Output: BMW is driving at 120 km/h
3. Encapsulation
Wrapping data (fields) and code (methods) into a single unit (class).
We use access modifiers (private, public, protected) to hide implementation details.
Example:
public class BankAccount
{
private double balance;
public void Deposit(double amount)
{
balance += amount;
}
public double GetBalance()
{
return balance;
}
}
Here, balance is hidden (private). It can only be accessed via methods.
4. Abstraction
Hiding implementation details and showing only essential features.
Achieved using abstract classes or interfaces.
Example:
public abstract class Shape
{
public abstract void Draw(); // abstract method (no body)
}
public class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing Circle");
}
}
5. Inheritance
One class can inherit properties and methods from another.
Promotes code reusability.
Example:
public class Animal
{
public void Eat() { Console.WriteLine("Eating..."); }
}
public class Dog : Animal
{
public void Bark() { Console.WriteLine("Barking..."); }
}
6. Polymorphism
One name, many forms → the ability of an object to behave differently based on context.
Types:
Compile-time (Overloading) → same method name, different parameters.
Runtime (Overriding) → derived class provides its own implementation.
Example (Method Overloading):
public class MathOperation
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}
Example (Method Overriding):
public class Animal
{
public virtual void Sound() { Console.WriteLine("Some sound"); }
}
public class Dog : Animal
{
public override void Sound() { Console.WriteLine("Bark"); }
}
Sandhiya PriyaPosted Oct 13, 2025, 7:03 AM
Object-Oriented Programming (OOP) Concepts in C#
OOP (Object-Oriented Programming) is a programming paradigm based on objects that encapsulate data (fields/properties) and behavior (methods). It helps structure code to be more modular, reusable, and maintainable. C# is a fully object-oriented language supporting all core OOP principles.
1. Class and Object
Class: A blueprint/template for creating objects. It defines properties, methods, and events.
Object: An instance of a class.
2. Encapsulation
Definition: Wrapping data and methods inside a class and restricting access to internal details.
Access modifiers:
private,public,protected, etc.Provides data security and controlled access.
3. Inheritance
Definition: Enables a class to inherit properties and methods from another class.
Dogcan useEat()fromAnimal.Types of Inheritance in C#:
Single Inheritance: One class inherits from one class.
Multilevel Inheritance: Class hierarchy, e.g.,
A -> B -> C.Hierarchical Inheritance: Multiple classes inherit from one base class.
4. Polymorphism
Definition: “Many forms” — the ability to perform the same action in different ways.
a. Compile-Time Polymorphism (Method Overloading)
b. Run-Time Polymorphism (Method Overriding)
5. Abstraction
Definition: Hiding complex details and showing only the essential features.
Achieved using abstract classes and interfaces.
6. Interfaces
Define a contract that classes must implement.
7. Constructors and Destructors
Constructor: Special method called when an object is created.
Destructor: Called when an object is destroyed (rarely used in C# because of garbage collection).
8. Properties
Provide getters and setters for fields.
9. Access Modifiers
Control visibility of classes and members:
public,private,protected,internal,protected internal,private protected.10. Composition
A class can have objects of other classes to reuse functionality.
Key Advantages of OOP
Reusability: Inheritance allows code reuse.
Encapsulation: Protects data and implementation.
Flexibility: Polymorphism allows different implementations.
Maintainability: Easy to modify and extend.
Modularity: Classes and objects divide code logically.
Sam HobbsPosted Aug 26, 2025, 8:37 PM
Object Oriented Programming is not a system, it is a paradigm or model. The term system implies something specific, such as a specific application or other software. People use the acronym OOPS and then other people try to give the acronym meaning but the correct acronym is OOP.
Jignesh KumarPosted Aug 24, 2025, 10:24 AM
Here is my article where I have covered OOPS with a Practical Example
other SOLID principle Articles.
Single Responsibility principle(SRP)
Open/Closed Principle in C#
Liskov Substitution Principle (LSP)
Ajay BansodePosted Aug 20, 2025, 5:46 PM
OOPs (Object-Oriented Programming System) concepts are the fundamental principles of object-oriented programming, mainly used in languages like C#, Java, Python, C++ etc.
Here are the main OOPs concepts with simple explanation and examples:
1. Class
A class is a blueprint or template for creating objects.
It defines properties (variables/fields) and behaviors (methods/functions).
Example (C#):
2. Object
An object is an instance of a class.
It represents a real-world entity.
Example:
3. Encapsulation
Wrapping data (fields) and code (methods) into a single unit (class).
We use access modifiers (
private,public,protected) to hide implementation details.Example:
Here,
balanceis hidden (private). It can only be accessed via methods.4. Abstraction
Hiding implementation details and showing only essential features.
Achieved using abstract classes or interfaces.
Example:
5. Inheritance
One class can inherit properties and methods from another.
Promotes code reusability.
Example:
6. Polymorphism
One name, many forms → the ability of an object to behave differently based on context.
Types:
Compile-time (Overloading) → same method name, different parameters.
Runtime (Overriding) → derived class provides its own implementation.
Example (Method Overloading):
Example (Method Overriding):
Summary of OOPs Concepts:
Class – Blueprint
Object – Instance
Encapsulation – Data hiding
Abstraction – Hiding implementation
Inheritance – Code reusability
Polymorphism – One name, many forms
Cynthia SathuragiriPosted Aug 19, 2025, 4:33 AM
OOPS = Object-Oriented Programming System
It’s a way of writing programs using objects (like real-life things).
1. Class → A blueprint (like a recipe).
Example: A Car design.
2. Object → A real thing made from the class.
Example: A red Tesla car built using the Car design.
3. Encapsulation → Keep things together (data + actions).
Example: A car’s steering, brakes, and engine are all inside the car.
4. Abstraction → Show only what’s needed, hide details.
Example: You just press the start button; you don’t see how the engine works inside.
5.Inheritance → Pass on features from one class to another.
Example: An ElectricCar is still a Car, but with extra features like a battery.
6.Polymorphism → Same action, different behavior.
Example: The word “start” means different things
Start a Car → engine starts
Start a Bike → bike starts
RitikPosted Aug 18, 2025, 11:52 AM
classes, objects, inheritance, encapsulation, abstraction, and polymorphism