๐๐ฎ๐๐ฎ ๐๐ฐ๐ฐ๐ฒ๐๐ ๐ ๐ผ๐ฑ๐ถ๐ณ๐ถ๐ฒ๐ฟ๐ ๐ณ๐ผ๐ฟ ๐๐ฒ๐ด๐ถ๐ป๐ป๐ฒ๐ฟ๐
Access modifiers control who sees your code. They keep your programs safe and organized.
You have four types in Java:
- Public: Access from anywhere.
- Protected: Access within the same package or child classes.
- Default: Access within the same package only.
- Private: Access within the same class only.
Look at this example. You have two packages: salem and madurai.
The Parent class lives in salem. It has a public pin and protected methods.
A Child class in the same package accesses public and protected items.
A Child class in a different package accesses public and protected items through inheritance.
Here is how access works:
- Same class: All modifiers work.
- Same package: Public, protected, and default work.
- Different package (child class): Public and protected work.
- Different package (non-child class): Only public works.
Source: https://dev.to/vinayagam_6a170db9281d526/access-modifiers-in-java-explained-for-beginners-121h