Â
Access Specifiers or (Access Modifiers) are special keywords in JAVA or any other Object Oriented Programming languages (C++, JAVA, PYTHON, etc.)Â
Access specifiers of access modifiers in java set the accessibility of classes, methods (member functions of a class), member variables and all other members of class. They are used to facilitate the encapsulation of components.
Types of Access Modifiers or Access Specifiers in JAVA:-
1. default
2. public
3. private
4. protected
Now, lets understand these 4 access specifiers one by one:-
1. default:-
->default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
->A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
->If we do not explicitly specify any access modifier for classes, methods, variables, etc, then by default the default access modifier is considered.
Example:-
//This is package can be named on your choice
package com.company;
//This is a class named as hfc
class hfc
{
//Below is the member function of class hfc
//Named as text having default access specifier/modifier
void text()
{
//This is a sample print statement
System.out.println("Welcome to Help For Coders!!");
}
}
//This is the main class of the program
public class Main
{
//This is the main function
public static void main(String[] args)
{
//Here Declaring the object of class "hfc" as 't'
//With new data-type
hfc t = new hfc();
//Calling the member function "text" of class 'hfc'
//By using the object of class 't'
t.text();
}
}
Output
2. public:-
->When methods, variables, classes, and so on are declared public, then we can access them from anywhere. The public access modifier has no scope restriction.
->A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.
->However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.
Example:-
//This is package can be named on your choice
package com.company;
//This is a class named as name
class name
{
//Below is the member variable of class "name"
//Named as 'your_name' having public access specifier/modifier
public String your_name;
}
//This is the main class of the program
public class Main
{
//This is the main function
public static void main(String[] args)
{
//Here Declaring the object of class "name" as 'n1,n2,n3'
//With new data-type
name n1 = new name();
name n2 = new name();
name n3 = new name();
//Calling the member variable "your_name"of class name
//Using the objects n1,n2,n3
n1.your_name="Tim Berners Lee";
n2.your_name="James Gosling";
n3.your_name="Bjarne Stroustrup";
//Printing the names in print statement
//Using the objects associated to the class 'name'
System.out.println(n1.your_name);
System.out.println(n2.your_name);
System.out.println(n3.your_name);
}
}
Output
3. private:-
->Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.
->When variables and methods are declared private, they cannot be accessed outside of the class.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
->Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.
->Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.
Example:-
//This is package can be named on your choice
package com.company;
//This is a class named as name
class name
{
//Below is the member variable of class "name"
//Named as 'your_name' having private access specifier/modifier
private String your_name;
}
//This is the main class of the program
public class Main
{
//This is the main function
public static void main(String[] args)
{
//Here Declaring the object of class "name" as 'n'
//With new data-type
name n = new name();
//Calling the member variable "your_name" of class 'name'
//By using the object of class 'n'
n.your_name = "Help For Coders";
}
}
Output
You should be wondering, if we want to access the private member variables or member functions. In this case we should use getters and setters/putters method. This means we should create two functions one to take the input and other one for the output.
 Â
Example:-
//This is package can be named on your choice
package com.company;
//This is a class named as name
class name
{
//Below is the member variable of class "name"
//Named as 'your_name' having private access specifier/modifier
private String your_name;
//getters method
public String getName()
{
return this.your_name;
}
//setters/putters method
public void setName(String your_name)
{
this.your_name = your_name;
}
}
//This is the main class of the program
public class Main
{
//This is the main function
public static void main(String[] args)
{
//Here Declaring the object of class "name" as 'n1,n2,n3'
//With new data-type
name n1 = new name();
name n2 = new name();
name n3 = new name();
//Calling the member function/method "setName" of class 'name'
//By using the object of class 'n1,n2,n3'
n1.setName("Amit Chaurasiya");
n2.setName("Abhishek Tiwari");
n3.setName("Jay Bhanushali");
//Printing the names using the getName method of class "name"
//By using the objects of the class as n1,n2,n3
System.out.println(n1.getName());
System.out.println(n2.getName());
System.out.println(n3.getName());
}
}
Output
4. protected:-
->Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
->When methods and data members are declared protected, we can access them within the same package as well as from subclasses.
->The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
->Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
Example:-
//This is package can be named on your choice
package com.company;
//This is a class named as "Languages"
class Languages
{
//Below is the member function of class "Languages"
//Named as 'show' having protected access specifier/modifier
protected void show()
{
System.out.println("Your are learning JAVA");
System.out.println("I am used for many purposes such as to build Android Apps!!");
}
}
//This is the main class of the program
class Main
{
//This is the main function
public static void main(String[] args)
{
//Here Declaring the object of class "Languages" as 'l'
//With new data-type
Languages l = new Languages();
//Calling the method 'show' of class 'Languages'
l.show();
}
}
Output
From above examples you must have understood all the Access specifiers/modifiers. Given below is the table for better understanding for which access modifier has how much scope.
Same Class | Same Package Sub-Class | Same Package Non-Sub-Class | Different Package Sub-Class | Different Package Non-Sub Class | |
---|---|---|---|---|---|
default | YES | YES | YES | NO | NO |
public | YES | YES | YES | YES | YES |
private | YES | NO | NO | NO | NO |
protected | YES | YES | YES | YES | NO |
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP