What are access modifiers in Java? Name and explain the types.
In Java, access modifiers determine the visibility or accessibility of classes, methods, variables, and other members in a program. They are used to encapsulate data and control how code interacts with different parts of a program.
Types of Access Modifiers
Java provides four main access modifiers:
Public
Members declared public are accessible from anywhere in the program, including from other packages.
Private
Members declared private are accessible only within the same class.
Provides strict encapsulation.
Protected
Members declared protected are accessible within the same package and by subclasses in other packages.
Default (Package-Private)
If no modifier is specified, the member is accessible only within the same package.
Access modifiers help enforce encapsulation and secure the design of object-oriented systems.