Modifiers in Java
Modifiers In Java
In Java, access modifiers are used to set the visibility of the classes, attributes, methods, etc. We can divide modifiers into two groups:
- Access Modifiers: It controls the access level
- Non- Access Modifiers: It doesn’t control access level but provides other functionality.
Access Modifiers
For classes:
Modifier | Description |
---|---|
public | The class is accessible by other class. |
default | The class is accessible by other class in the same package. |
For properties and methods:
Modifier | Description |
---|---|
public | Accessible by other class. |
default | Accessible by other class in the same package. |
private | Accessible within the same class. |
protected | Accessible within a package and subclass. |
Non- Access Modifiers
For classes:
Modifier | Description |
---|---|
final | The class cannot be inherited by other classes. |
abstract | The class cannot be used to create objects. |
For properties and methods:
Modifier | Description |
---|---|
final | Properties and methods cannot be overridden/ modified. |
static | Properties and methods belong to a class rather than an object. |
abstract | Can be used in the abstract class and can only be used on methods. |