Definition :
- Java Access Specifiers (also known as Visibility Specifiers ) regulate access to classes, fields and methods in Java.These Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access. Access Specifiers are an integral part of object-oriented programming.
Types Of Access Specifiers :
In java we have four Access Specifiers and they are listed below.
1. public
2. private
3. protected
4. default(no specifier)
We look at these Access Specifiers in more detail.
public specifiers :
Public Specifiers achieves the highest level of accessibility. Classes, methods, and fields declared as public can be accessed from any class in the Java program, whether these classes are in the same package or in another package.
Example :
private specifiers :
Private Specifiers achieves the lowest level of accessibility.private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier. Using Private Specifier we can achieve encapsulation and hide data from the outside world.
Example :
protected specifiers :
Methods and fields declared as protected can only be accessed by the subclasses in other package or any class within the package of the protected members' class. The protected access specifier cannot be applied to class and interfaces.
default(no specifier):
When you don't set access specifier for the element, it will follow the default accessibility level. There is no default specifier keyword. Classes, variables, and methods can be default accessed.Using default specifier we can access class, method, or field which belongs to same package,but not from outside this package.
Example :
Real Time Example :
This are the Access Specifier in java and we will learn in detail about these Access Specifier in future chapters.
- Java Access Specifiers (also known as Visibility Specifiers ) regulate access to classes, fields and methods in Java.These Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access. Access Specifiers are an integral part of object-oriented programming.
Types Of Access Specifiers :
In java we have four Access Specifiers and they are listed below.
1. public
2. private
3. protected
4. default(no specifier)
We look at these Access Specifiers in more detail.
public specifiers :
Public Specifiers achieves the highest level of accessibility. Classes, methods, and fields declared as public can be accessed from any class in the Java program, whether these classes are in the same package or in another package.
Example :
public class Demo { // public class
public x, y, size; // public instance variables
}
private specifiers :
Private Specifiers achieves the lowest level of accessibility.private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier. Using Private Specifier we can achieve encapsulation and hide data from the outside world.
Example :
public class Demo { // public class
private double x, y; // private (encapsulated) instance variables
public set(int x, int y) { // setting values of private fields
this.x = x;
this.y = y;
}
public get() { // setting values of private fields
return Point(x, y);
}
}
protected specifiers :
Methods and fields declared as protected can only be accessed by the subclasses in other package or any class within the package of the protected members' class. The protected access specifier cannot be applied to class and interfaces.
default(no specifier):
When you don't set access specifier for the element, it will follow the default accessibility level. There is no default specifier keyword. Classes, variables, and methods can be default accessed.Using default specifier we can access class, method, or field which belongs to same package,but not from outside this package.
Example :
class Demo
{
int i; (Default)
}
Real Time Example :
This are the Access Specifier in java and we will learn in detail about these Access Specifier in future chapters.
awesome............
ReplyDeleteabba abbba em septiri em septiri very nice explanation
ReplyDeletenice example for access specifiers
ReplyDeleteawesome , thanks
ReplyDeleteAwesome explanation with diagram. Thanks
ReplyDeleteso nice.... thanx
ReplyDeletenice example nd good discription........
ReplyDeletenice example and concept of access specifier "i like this concept"
ReplyDeleteAwesome... Thank You...
ReplyDeleteClean, clear, precise.
ReplyDeletesuperrrrr
ReplyDeleteCan we say reception as protected and hall as default.
ReplyDeletenice example and concept of access specifier "i like this concept"
ReplyDeleteSimple Answer Awesome!!!!!!!!!!!!!!!
ReplyDeletemaster at work
ReplyDeleteawsum
ReplyDeleteayyeeeee............
ReplyDeleteis it possible make a class as private???
ReplyDeleteevery people can understand this eamples
ReplyDeleteRight
Deletenice explanation
ReplyDeletenice explanation
ReplyDeletenice..
ReplyDeleteAWESOME ONE.............................
ReplyDeletenice explanation and easy to understand
ReplyDeleteVery useful thanks 🙂
ReplyDeletefantastics, explanation
ReplyDeleteThanks a lot!!!!!!!!! But I have a doubt. Isn't the fourth access specifier "friendly" and isn't the default access specifier "public"????
ReplyDeleteGood explanation
ReplyDeleteThere is no concept of specifiers in java all are considered as access modifiers....
ReplyDeleteTo Check the same give protected to a class name and the ide will show you private modifier is not allowed on top level classes........
Nice Thanks
ReplyDeletevery useful.. thank u...
ReplyDeleteUseful.. Contents
ReplyDeleteGreat explanation.
ReplyDeletenyc expalination
ReplyDeletethank U
ReplyDelete👍👌
ReplyDeletegud
ReplyDeletenice explaination,
ReplyDeletethank you so much
Nice...good example..easy to understand
ReplyDeleteNice explanation...good example
ReplyDelete😁😁tqzz fr undrstnd wid d bst exzmpl
ReplyDeleteHelped in exam a lot good explanation got full marks by coping it
ReplyDeleteVery easy explanation....all thanks to you.
ReplyDeleteI too like your example
brilliant example and explaination
ReplyDeleteCan u explain the diagram please.I am not able to understand
ReplyDeleteCan u explain the diagram.I am not able to understand
ReplyDeleteThaaliya thaaliya
ReplyDeleteThanks for sharing this Access Modifiers in Java
ReplyDeleteThanks for sharing this Articl
ReplyDeleteMurali chavadi:nice but improve explanation with examples that's helps better to understand
ReplyDeleteGood Explaination
ReplyDeleteIt's an awesome explaination
ReplyDelete