Access Modifiers in java
Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are;- Visible to the package, the default. No modifiers are needed.
- Visible to the class only (private).
- Visible to the world (public).
- Visible to the package and all subclasses (protected).
Example of Access Modifiers in java
Variables and methods can be declared without any modifiers, as in the following examplesString version = "1.5.1";
boolean processOrder()
{
return true;
}
Default Access Modifier
If you don't use any modifier, it is treated as default bydefault. The default modifier is accessible only within package.Private Access Modifier
The private access modifier is accessible only within class.Public Access Modifier
The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.Protected Access Modifier
The protected access modifier is accessible within package and outside the package but through inheritance only.
Note: Some website called Access Specifier in Java and some website provide tutorial Access Modifier in Java
No comments :
Post a Comment