Day 19 : Modifier

Chi-Tse Chiang
2022-10-04 23:59:27

#前言

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:

1. Access Modifiers

2. Non-Access Modifiers

-> The difference between the two is whether they have access permissions. Non-access-restricted modifiers do not have permissions, but provide other functional keywords.

Access Modifiers

Used with classes:

1. public: maximum scope, can be accessed by any class.

2. default: can be accessed by classes in the same package, but not by others. (Default if no modifier is specified)

Used on functions, properties, and variables:

1. public

2. default

3. 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.

4. 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.

Access Modifier

# Non-Access Modifiers

Used with class:

1. final : A class declared with this modifier cannot be inherited by other classes.

2. 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.

Used on functions, attributes, and variables:
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.