Access modifiers in java

preview_player
Показать описание
Follow me on Instagram
There are mainly 4 access modifier
public , private, default and protected
01:20 public modifier
03:10 private modifier
03:52 default modifier
05:00 protected modifier

if we are giving public as the access modifier to variables or methods then it can be accessed from everywhere
if we are giving private as the access modifier for variable then only the class where variable is declared will be able to access it. No other classes will be able to access it.
if we are not giving any modifier then by default it will be a default modifier - if we use default modifier then it can be accessed from all the classes in the same package
if we are giving protected modifier then it works same like default that means we can access the variable in any classes of the same package

Subclass or child class also will be able to access protected variable.
protected = default + child class
we need to create child class reference to access the variable

if some other classes need to use private variable then we need to use getter and setter method.

java access modifier
access modifiers in java
Рекомендации по теме