Java - Access modifiers - private, default, protected, public

Опубликовано: 10 Май 2026
на канале: World Gurukul way of Learning Values and Wisdom
870
2

The access modifiers, or inheritance modifiers, set the accessibility of classes, methods, and other members. Members marked as public can be reached from anywhere. If a class or its member does not have any modifiers, default access is assumed.
The access modifiers in java specifies accessibility of a class, constructor, method or data member.
Java access modifiers are:
• private
• default
• protected
• public
There are non-access modifiers such as static, abstract etc. which we will learn in next sessions.
This matrix table shows whether code within a class has access to the class or method depending on the accessing class location and the modifier for the accessed class or class member:
The private access modifier is accessible only within class / same class.
If any class or constructor is private, instance of that class cannot be created from outside the class.
When no modifier is specified, it is default modifier applied by default. The default modifier is accessible only within package.
The protected access modifier is accessible within package and outside the package.
The protected access modifier can be applied on the data member, method and constructor but can't be applied on the class
The public access modifier is accessible from everywhere for enabling everyone to use functionality with out any restrictions.