Modifiers in Java are divided into two categories: access modifiers and non-access modifiers.
The accessibility or scope of a field, method, constructor, or class is defined by the access modifiers in Java. The access modifier can be used to change the access level of fields, constructors, methods, and classes.
There are four types of Java access modifiers:
Access Modifier | within class | within package | outside package by subclass only | outside package |
---|---|---|---|---|
Private | Yes | No | No | No |
Default | Yes | Yes | No | No |
Protected | Yes | Yes | Yes | No |
Public | Yes | Yes | Yes | Yes |
The private access modifier is accessible only within the class.
Example:
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
If no modifier is specified, it is assumed to be the default. The default modifier is only available within the package. It is not accessible from the outside of the package. It allows for more access than private. However, it is more restricted than protected and open.
Example:
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
The protected access modifier can be used both inside and outside of the package, but only through inheritance.
The data member, method, and constructor can all have the protected access modifier applied to them. It is not applicable to the class.
It is more accessible than the standard modifier.
Example:
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
The public access modifier can be used anywhere. It is the most versatile of all the modifiers.
Example:
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Note: also read about the Constructor in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…