When faced with large-scale projects or practical needs, the most common approach is to collaborate with others using collaboration methods, which can improve the reliability of the code and ease of maintenance. However, in order to ensure that intellectual property or some confidential corporate data is not excessively accessed, programmers will use modifiers to restrict the scope of access to or from various types, functions, attributes, etc.
The main purpose is to use front-end modifiers to control what others can see, thereby protecting the security of information and programs.
Modifiers are further divided into two categories:
public
: maximum scope, can be accessed by any class.default
: can be accessed by classes in the same package, but not by others. (Default if no modifier is specified)public
default
private
: has the smallest scope and can only be accessed by the same class. It is usually used for encapsulation.public class First{
private String name = "Chi";
private String location = "Taipei";
private int age = 20;
static void student(){
System.out.println("Your're junior.");
}
}
public class Second{
public static void main(String[] args){
System.out.println(First.name); //error
System.out.println(First.location); //error
System.out.println(First.age); //error
First.student(); //error
}
}
-> Because the modifier of the variable in class First is private
, it cannot be used in another class.
protected
: can be accessed by subclasses that extend
the class in addition to classes in the same package.public class Student {
protected String name = "Chi";
protected int grade = 41;
protected char gpa = 'E';
}
class college extends Student {
private int graduation = 2024;
public static void main(String[] args) {
college newobj = new college();
System.out.println("Name: " + newobj.name);
System.out.println("Grade: " + newobj.grade);
System.out.println("gpa: " + newobj.gpa);
System.out.println("Graduation Year: " + newobj.graduation);
}
}
-> The variable modifier in the first category is protected, so college, which inherits it, can use it directly.
final
: A class declared with this modifier cannot be inherited by other classes.abstract
: This declared class cannot be used to create objects, meaning it has no constructors, and its contents can be stored by inheritance.abstract class Main {
public String name;
public abstract void salary(); // abstract method
}
/*abstract Main(){ //Constructor : error
String name = "Jason"
}*/
// Subclass (inherit from Main)
class employee extends Main {
String name = "Jason"
public void salary() {
System.out.println("Salary : 45000");
}
}
Program analysis: Because the types we declare with the abstract modifier cannot create objects, we can only set the properties of variable functions first by inheritance, and then set the objects in different types.
final
, static
, abstract
, transient
, synchronized
, and volatile
-> Some of these keywords are not commonly used, so I won't explain them here. If you're interested, you can refer to the introduction on the EDUCBA official website.