OOPS
Object-Oriented Programming concepts
0 / 12 revised
4 Pillars of OOP ๐ฏ
OOPS
โพ
Quick Recall
A PIE โ Abstraction, Polymorphism, Inheritance, Encapsulation
| Pillar | Definition | Example |
|---|---|---|
| Abstraction | Hide complexity, show only essentials | Abstract class Shape with abstract area() |
| Encapsulation | Bundle data + methods; restrict access via access modifiers | Private fields with public getters/setters |
| Inheritance | Child class acquires properties/methods of parent | Dog extends Animal |
| Polymorphism | Same interface, different behavior | animal.speak() โ Dog barks, Cat meows |
Class vs Object, Constructors ๐ฏ
OOPS
โพ
Quick Recall
Class = blueprint, Object = instance; constructor initializes
Class: Template defining attributes + methods. Object: Runtime instance of a class.
Constructor: Special method called on object creation. Initializes state.
Destructor: Called when object is destroyed (Python: __del__, Java: finalize โ rarely used, GC handles it).
Python:
Java:
Constructor: Special method called on object creation. Initializes state.
Destructor: Called when object is destroyed (Python: __del__, Java: finalize โ rarely used, GC handles it).
Python:
def __init__(self, name): self.name = nameJava:
public MyClass(String name) { this.name = name; }Inheritance Types & Diamond Problem ๐ฏ
OOPS
โพ
Quick Recall
Single, Multi-level, Hierarchical; Diamond = ambiguity in multiple inheritance
| Type | Description | Support |
|---|---|---|
| Single | A โ B | Java, Python |
| Multi-level | A โ B โ C | Java, Python |
| Hierarchical | A โ B, A โ C | Java, Python |
| Multiple | A,B โ C | Python (MRO), NOT Java (use interfaces) |
| Hybrid | Mix of above | Python only |
Resolution: Python uses MRO (C3 linearization). Java avoids it โ no multiple class inheritance, use interfaces with default methods.
Compile-time vs Runtime Polymorphism ๐ฏ
OOPS
โพ
Quick Recall
Overloading = compile-time (same name, diff params); Overriding = runtime (child redefines parent method)
| Overloading (Compile-time) | Overriding (Runtime) | |
|---|---|---|
| Binding | Early/static binding | Late/dynamic binding |
| Method | Same name, different parameters | Same signature in child class |
| Inheritance | Not required | Required |
| Example | add(int,int) vs add(float,float) | Child.speak() overrides Parent.speak() |
Abstract Class vs Interface ๐ฏ
OOPS
โพ
Quick Recall
Abstract = partial implementation + state; Interface = pure contract (pre-Java 8)
| Abstract Class | Interface | |
|---|---|---|
| Methods | Abstract + concrete | All abstract (Java 8+: default methods) |
| Variables | Any type | public static final only |
| Inheritance | Single only | Multiple allowed |
| Constructor | Yes | No |
| When to use | IS-A with shared code | CAN-DO capability contract |
Access Modifiers
OOPS
โพ
Quick Recall
public > protected > default > private (Java); Python uses _ convention
Java:
Python convention: _protected, __private (name mangling), no true enforcement.
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | โ | โ | โ | โ |
| protected | โ | โ | โ | โ |
| default | โ | โ | โ | โ |
| private | โ | โ | โ | โ |
Static vs Instance Members
OOPS
โพ
Quick Recall
Static = shared across all objects (class-level); Instance = per-object
Static: Belongs to class, not instance. Shared memory. Access via ClassName.method().
Instance: Each object has its own copy.
Use static for: Utility methods, constants, counters, factory methods.
Java:
Python: Class variable defined outside __init__, or @staticmethod / @classmethod.
Instance: Each object has its own copy.
Use static for: Utility methods, constants, counters, factory methods.
Java:
static int count = 0;Python: Class variable defined outside __init__, or @staticmethod / @classmethod.
this/self & super
OOPS
โพ
Quick Recall
this = current object ref; super = parent class ref
this (Java) / self (Python): Reference to current instance. Resolves ambiguity between field and parameter names. Required in Python method signatures.
super: Calls parent class constructor or methods.
Java:
Python:
super: Calls parent class constructor or methods.
Java:
super(args) must be first line in constructor.Python:
super().__init__(args)Association, Aggregation, Composition ๐ฏ
OOPS
โพ
Quick Recall
Association = uses; Aggregation = has-a (weak); Composition = owns (strong, lifecycle dependent)
| Relationship | Strength | Lifecycle | Example |
|---|---|---|---|
| Association | Weak | Independent | Teacher โ Student |
| Aggregation | Medium (HAS-A) | Independent (part survives) | Department โ Professor |
| Composition | Strong (OWNS) | Dependent (part dies with whole) | House โ Room |
SOLID Principles ๐ฏ
OOPS
โพ
Quick Recall
SRP, OCP, LSP, ISP, DIP โ each principle = one sentence + one example
| Principle | Rule | Example |
|---|---|---|
| Single Responsibility | A class has one reason to change | Separate UserAuth from UserProfile |
| Open/Closed | Open for extension, closed for modification | Add new shapes via subclass, don't modify Shape |
| Liskov Substitution | Subtypes must be substitutable for base types | Square shouldn't break Rectangle's setWidth contract |
| Interface Segregation | Many specific interfaces > one fat interface | Printable, Scannable vs one MachineInterface |
| Dependency Inversion | Depend on abstractions, not concretions | Service depends on Repository interface, not MySQLRepo |
Design Patterns (Interview Subset) ๐ฏ
OOPS
โพ
Quick Recall
Singleton = one instance; Factory = delegate creation; Observer = pub-sub; Strategy = swap algorithms
Singleton: One instance globally. Private constructor + static getInstance().
Factory: Create objects without specifying exact class.
Observer: Subject notifies observers on state change. Used in event systems, React state, MVC.
Strategy: Encapsulate algorithms, swap at runtime. E.g., SortStrategy with BubbleSort, MergeSort implementations โ caller doesn't care which.
class Singleton: _inst=None; @classmethod; def get(cls): cls._inst=cls._inst or cls(); return cls._instFactory: Create objects without specifying exact class.
ShapeFactory.create("circle") returns Circle().Observer: Subject notifies observers on state change. Used in event systems, React state, MVC.
Strategy: Encapsulate algorithms, swap at runtime. E.g., SortStrategy with BubbleSort, MergeSort implementations โ caller doesn't care which.
OOP vs Procedural
OOPS
โพ
Quick Recall
OOP = objects + encapsulation + reuse; Procedural = functions + sequential
| OOP | Procedural | |
|---|---|---|
| Unit | Object (data + behavior) | Function |
| Data | Encapsulated, protected | Shared, exposed |
| Reuse | Inheritance, composition | Function calls |
| Extensibility | Easy (add new classes) | Harder (modify functions) |
| Examples | Java, Python, C++ | C, Pascal |