OOP Principles

#oop #objectorientedprinciples
Basic OOP Concepts Explained with Clear Examples:
1. 𝐄𝐧𝐜𝐚𝐩𝐬𝐮𝐥𝐚𝐭𝐢𝐨𝐧 Hide internal data behind public methods. - Example: A BankAccount class keeps balance and pin private. The only way to interact with it is through deposit() and getBalance().
2. 𝐀𝐛𝐬𝐭𝐫𝐚𝐜𝐭𝐢𝐨𝐧 Expose a simple interface, hide the complexity behind it. - Example: An EmailService class gives you sendEmail(to, body). Internally, it handles SMTP connections, authentication, and retry logic. The caller doesn't need to know any of that. They just call one method and it works.
3. 𝐈𝐧𝐡𝐞𝐫𝐢𝐭𝐚𝐧𝐜𝐞 Let child classes reuse and override behavior from a parent class. - Example: An Animal class defines speak(). Dog extends it and returns “Woof!”, Cat extends it and returns “Meow!”. Shared logic lives in one place, and each subclass customizes what it needs.
4. 𝐏𝐨𝐥𝐲𝐦𝐨𝐫𝐩𝐡𝐢𝐬𝐦 Write code that works with multiple types through a common interface. - Example: Define a Shape interface with a draw() method. Now Circle, Rectangle, and Triangle each implement draw() their own way. A single drawShape(Shape s) method works with all of them.

