iT邦幫忙

2022 iThome 鐵人賽

DAY 19
0

#前言

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.

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

https://ithelp.ithome.com.tw/upload/images/20221004/20151216uzg7jLFj99.png


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


上一篇
Day 18 : Constructors
下一篇
Day 20 : Encapsulation
系列文
30天Java由淺入深30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言