#05 Java Access Modifiers private, public, protected, default | Understand Java Access Modifiers

Опубликовано: 06 Октябрь 2024
на канале: Sachii Online Academy
153
6

Please Subscribe for more!
#05 Java Access Modifiers | Understand Java Access Modifiers with demo | OOP using Java
Access modifiers are keywords that can be used to control the visibility of fields, methods, and constructors in a class.
The four access modifiers in Java are public, protected, default, and private.

Four Types of Access Modifiers
Private: We can access the private modifier only within the same class and not from outside the class.
Default: We can access the default modifier only within the same package and not from outside the package. And also, if we do not specify any access modifier it will automatically consider it as default.
Protected: We can access the protected modifier within the same package and also from outside the package with the help of the child class. If we do not make the child class, we cannot access it from outside the package. So inheritance is a must for accessing it from outside the package.
Public: We can access the public modifier from anywhere. We can access public modifiers from within the class as well as from outside the class and also within the package and outside the package.