𝗝𝗮𝘃𝗮 𝗘𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗶𝗼𝗻 𝗚𝘂𝗶𝗱𝗲
You need to protect your data in Java. This is encapsulation.
Access modifiers control who sees your code.
- Public: Everyone sees it.
- Private: Only the owner class sees it.
- Default: Only the same package sees it.
- Protected: The same package and sub classes see it.
Use getters and setters to manage this.
Example: A gold shop. The gold price is private. A customer uses a getter to see the price. A customer uses a setter to change the price.
You set rules in the setter. You stop people from setting a price too low.
Encapsulation keeps your data safe. It gives you control.