Comprehensive OOP Notes
This guide covers core OOP concepts, practical examples, best practices, and design principles useful for interviews and exams.
Classes & Objects
Class: blueprint describing data (attributes) and behavior (methods). Object: instance of a class. Constructors initialize object state. Destructors cleanup (language-specific).
// Example (Java-like)\nclass Person {\n String name; int age;\n Person(String n, int a){ name=n; age=a; }\n}\n
Encapsulation
Encapsulation hides internal state and exposes behavior via methods. Use access modifiers (private, protected, public). Benefits: modularity, maintainability.
Abstraction
Abstraction exposes essential features and hides implementation details. Achieved via abstract classes and interfaces. Focuses on 'what' rather than 'how'.
Inheritance
Inheritance models 'is-a' relationships. Single, multiple (language-dependent), multilevel, hierarchical. Use super/parent classes for shared behavior. Beware tight coupling.
Polymorphism
Polymorphism allows entities to take multiple forms. Compile-time (method overloading, operator overloading) and runtime (method overriding via virtual methods).
Interfaces & Abstract Classes
Interfaces define contracts (method signatures) without implementation (some languages allow default methods). Abstract classes can include partial implementation and abstract methods.
Constructors & Destructors
Constructors initialize objects. Overloaded constructors provide multiple ways to create objects. Destructors or finalizers are used for resource release (use RAII in C++).
Method Overloading vs Overriding
Overloading: same method name, different parameters (compile-time). Overriding: subclass provides specific implementation (runtime polymorphism).
SOLID Principles (short)
- S — Single Responsibility
- O — Open/Closed
- L — Liskov Substitution
- I — Interface Segregation
- D — Dependency Inversion
Design Patterns (common)
Factory, Singleton, Strategy, Observer, Decorator, Adapter — patterns solve recurring design problems.
Memory & Performance
Be aware of object creation cost, garbage collection pauses, prefer composition over inheritance when appropriate, avoid deep inheritance hierarchies.