OBJECT ORIENTED PROGRAMMING (OOP) — CORE CONCEPTS¶
1. ENCAPSULATION¶
Definition¶
-
Encapsulation is the process of hiding internal state and exposing only controlled access
-
Achieved using:
-
access modifiers (
private,public,protected) -
properties
-
methods
-
Goal¶
-
Protect data integrity
-
Control how state changes
Example¶
class BankAccount
{
private decimal _balance;
public void Deposit(decimal amount)
{
if (amount <= 0) throw new Exception();
_balance += amount;
}
public decimal GetBalance()
{
return _balance;
}
}
What is hidden?¶
_balance → not directly accessible
Only controlled via methods
Bad Example¶
class BankAccount
{
public decimal Balance;
}
Anyone can modify → no control
Interview Answer¶
Encapsulation is the practice of restricting direct access to an object's internal state and exposing controlled operations. It ensures data integrity and enforces business rules through well-defined interfaces.
2. ABSTRACTION¶
Definition¶
-
Abstraction hides implementation details
-
Exposes only essential behavior
Achieved via¶
-
Interfaces
-
Abstract classes
Example¶
interface IPayment
{
void Pay(decimal amount);
}
class CreditCardPayment : IPayment
{
public void Pay(decimal amount)
{
// complex logic hidden
}
}
What is hidden?¶
Validation
API calls
Processing logic
Consumer view¶
IPayment payment = new CreditCardPayment();
payment.Pay(100);
Interview Answer¶
Abstraction focuses on exposing only relevant behavior while hiding implementation details. It allows developers to work with high-level concepts without needing to understand underlying complexity.
3. ENCAPSULATION vs ABSTRACTION¶
Difference¶
| Encapsulation | Abstraction |
|---|---|
| Hides data | Hides complexity |
| Focus: state | Focus: behavior |
| Uses access modifiers | Uses interfaces/abstract classes |
Combined Example¶
class Car
{
private Engine _engine;
public void Start()
{
_engine.Ignite();
}
}
Encapsulation → engine hidden
Abstraction → Start() hides complexity
Interview Answer¶
Encapsulation and abstraction complement each other. Encapsulation protects internal state, while abstraction simplifies interaction by exposing only necessary behavior.
4. INHERITANCE¶
Definition¶
-
Mechanism to reuse code
-
Derived class inherits from base class
Example¶
class Animal
{
public void Eat() => Console.WriteLine("Eating");
}
class Dog : Animal
{
public void Bark() => Console.WriteLine("Barking");
}
Usage¶
var dog = new Dog();
dog.Eat();
dog.Bark();
Types in C¶
-
Single inheritance (classes)
-
Multiple inheritance via interfaces
Interview Answer¶
Inheritance allows a class to reuse and extend the behavior of another class. It promotes code reuse and establishes an “is-a” relationship between types.
5. POLYMORPHISM¶
Definition¶
-
Ability to treat objects differently based on type
-
Same interface → different behavior
Types¶
1. Compile-time (method overloading)¶
class Math
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}
2. Runtime (method overriding)¶
class Animal
{
public virtual void Speak() => Console.WriteLine("Animal");
}
class Dog : Animal
{
public override void Speak() => Console.WriteLine("Dog");
}
Animal a = new Dog();
a.Speak(); // Dog
Mechanism¶
Resolved at runtime using virtual table (vtable)
Interview Answer¶
Polymorphism allows objects to be treated as instances of their base type while executing behavior specific to their actual type. Runtime polymorphism is achieved through virtual and override methods.
6. virtual vs override vs new¶
virtual¶
- Allows method to be overridden
public virtual void Print() { }
override¶
-
Overrides base implementation
-
Runtime binding
public override void Print() { }
new¶
-
Hides base method
-
Compile-time binding
public new void Print() { }
Example¶
class A
{
public virtual void Show() => Console.WriteLine("A");
}
class B : A
{
public override void Show() => Console.WriteLine("B");
}
A obj = new B();
obj.Show(); // B
class B : A
{
public new void Show() => Console.WriteLine("B");
}
A obj = new B();
obj.Show(); // A
Interview Answer¶
Virtual and override enable runtime polymorphism, where method resolution depends on the object type. The new keyword hides the base method and resolves based on the reference type.
7. INTERFACE vs ABSTRACT CLASS¶
Definition¶
Interface¶
-
Contract only
-
No state (mostly)
-
Multiple inheritance supported
Abstract Class¶
-
Partial implementation
-
Can have state
-
Single inheritance
Example¶
interface IShape
{
double Area();
}
abstract class Shape
{
public abstract double Area();
public void Print() => Console.WriteLine("Shape");
}
Implementation¶
class Circle : Shape, IShape
{
public override double Area() => 3.14 * 10 * 10;
}
Differences¶
| Feature | Interface | Abstract |
|---|---|---|
| Methods | No implementation | Can have |
| State | No | Yes |
| Inheritance | Multiple | Single |
Interview Answer¶
Interfaces define contracts and support multiple inheritance, while abstract classes provide shared implementation and state. Interfaces are used for capability, while abstract classes represent base behavior.
8. IS-A vs HAS-A¶
IS-A (Inheritance)¶
class Dog : Animal
Dog IS-A Animal
HAS-A (Composition)¶
class Car
{
Engine engine;
}
Car HAS-A Engine
Interview Answer¶
Inheritance represents an “is-a” relationship, while composition represents a “has-a” relationship. Composition is generally preferred for flexibility and loose coupling.
9. COMPOSITION vs INHERITANCE¶
Example — Composition¶
class Engine
{
public void Start() { }
}
class Car
{
private Engine _engine = new Engine();
public void Start()
{
_engine.Start();
}
}
Benefits¶
-
Loose coupling
-
Flexible
-
Replaceable components
Interview Answer¶
Composition is preferred over inheritance as it promotes loose coupling and flexibility by combining behaviors instead of tightly binding class hierarchies.
10. SOLID LINK (OOP extension)¶
SRP¶
- One responsibility
OCP¶
- Open for extension, closed for modification
LSP¶
- Derived class should replace base safely
ISP¶
- Small interfaces
DIP¶
- Depend on abstractions
Interview Answer¶
SOLID principles extend OOP concepts by guiding design toward maintainable, scalable, and loosely coupled systems.
FINAL SUMMARY¶
Interview Answer¶
OOP is based on encapsulation, abstraction, inheritance, and polymorphism. Encapsulation protects data, abstraction hides complexity, inheritance enables reuse, and polymorphism allows flexible behavior. Together, these principles help design maintainable and scalable systems.