OOPs Principles
1. Encapsulation – "Data hiding"
- Binding data (variables) and methods (functions) together in a class.
- Keeps the internal state safe from outside interference.
✅ Achieved using private
variables and public
getter/setter methods.
2. Abstraction – "Show only what’s needed"
- Hides complex implementation and shows only essential features.
- Focus on what an object does, not how it does it.
✅ Achieved using abstract classes and interfaces.
3. Inheritance – "Code reuse"
- One class (child) inherits properties and methods from another class (parent).
- Promotes code reusability and hierarchy.
✅ Use the extends
keyword.
4. Polymorphism – "Many forms"
- Ability to take many forms: same method behaves differently in different classes.
- Two types: Compile-time (method overloading) and Run-time (method overriding)
🔚 In Short:
Principle | Purpose |
---|---|
Encapsulation | Protect data |
Abstraction | Hide complexity |
Inheritance | Reuse code |
Polymorphism | One method, many forms |