Java Modifiers
Modifiers are keywords that we add to those definitions to change their meanings.
There are Two types of modifiers in Java.
a) Access Modifiers
b) Non-access Modifiers
———————
a) Access Modifiers
There are 4 types of modifiers in Java.
we use access modifiers to define access control for classes, methods and variables etc…
1) private
2) default
3) protected
4) public
———————
1) private:
The private access modifier is accessible only within class.
Ex:
class A {
private int a = 40;
.
.
———————
2) default
If we don’t use any modifier, it is treated as default by default. this can be accessible only within package.
Ex:
package abc;
class Sample {
int a;
.
.
———————
3) protected
The protected access modifier is accessible within package, and out side of the package but through Inheritance only.
Ex:
———————
4) public
public access modifier is accessible everywhere. It has widest scope among all other modifiers.
Ex:
public Class A{
public static void main (String [] args) {
.
.
Modifier | Within class | Within Package | Outside of the Package (by sub classes) | Outside of the Package |
Private | Y | N | N | N |
Default | Y | Y | N | N |
Protected | Y | Y | Y | N |
Public | Y | Y | Y | Y |
b) Non-access Modifiers
Java provides no of non access modifiers to achieve many other functionality.
Static modifier to create class, methods and variables.
Ex:
class Abc {
static String name =”Selenium”
.
.
——————————–
final modifier for finalizing the implementation of classes, methods and variables.
Ex:
class Sample {
final int a =100;
————————-
abstract modifier is to create abstract classes and methods.
public abstract Sample {
.
.
etc…
—————————————
Also Read:
Java Variables
Java Operators
Java Conditional Statements
Java Loop Statements
Java Arrays
Java Methods
Follow me on social media: