16. final keyword and permission modifier

01_final keyword concept and four usages

The final keyword represents the final and unalterable.

There are four common uses:

  1. Can be used to decorate a class
  2. Can be used to modify a method
  3. It can also be used to modify a local variable
  4. It can also be used to modify a member variable

02_ The final keyword is used to decorate a class

When the final keyword is used to modify a class, the format is:
public final class class name{
// ...
}

Meaning: the current class cannot have any subclasses. (eunuch)
Note: if a class is final, all member methods in it cannot be overridden (because there is no son.)

public final class MyClass /*extends Object*/ {

    public void method() {
        System.out.println("Method execution!");
    }

}
// You cannot use a final class as a parent
public class MySubClass /*extends MyClass*/ {
}

03_ The final keyword is used to decorate member methods

When the final keyword is used to modify a method, the method is the final method, that is, it cannot be overridden.
Format:
Modifier final return value type method name (parameter list){
//Method body
}

matters needing attention:
For classes and methods, the abstract keyword and the final keyword cannot be used at the same time because of contradictions.

public abstract class Fu {

    public final void method() {
        System.out.println("Parent class method execution!");
    }

    public abstract /*final*/ void methodAbs() ;

}

public class Zi extends Fu {
    @Override
    public void methodAbs() {

    }

    // Wrong writing! Cannot override the final method in the parent class
//    @Override
//    public void method() {
//        System.out.println("subclass overrides method overriding parent!");
//    }
}

04_ The final keyword is used to decorate local variables

/*
final Keywords represent final and unchangeable.

There are four common uses:
1. Can be used to decorate a class
2. Can be used to modify a method
3. It can also be used to modify a local variable
4. It can also be used to modify a member variable
 */
public class Demo01Final {

    public static void main(String[] args) {
        int num1 = 10;
        System.out.println(num1); // 10
        num1 = 20;
        System.out.println(num1); // 20

        // Once final is used to modify a local variable, the variable cannot be changed.
        // "One assignment, unchanged for life"
        final int num2 = 200;
        System.out.println(num2); // 200

//        num2 = 250; //  Wrong writing! Can't change!
//        num2 = 200; //  Wrong writing!

        // Write correctly! As long as there is a unique assignment
        final int num3;
        num3 = 30;

        // For basic types, immutability means that the data in variables cannot be changed
        // For reference types, immutability means that the address value in the variable cannot be changed
        Student stu1 = new Student("Zhao Liying");
        System.out.println(stu1);
        System.out.println(stu1.getName()); // Zhao Liying
        stu1 = new Student("Huo Jianhua");
        System.out.println(stu1);
        System.out.println(stu1.getName()); // Huo Jianhua
        System.out.println("===============");

        final Student stu2 = new Student("Gao Yuanyuan");
        // Wrong writing! The reference type variable of final, in which the address cannot be changed
//        stu2 = new Student("Zhao Youting");
        System.out.println(stu2.getName()); // Gao Yuanyuan
        stu2.setName("Gao Yuanyuan");
        System.out.println(stu2.getName()); // Gao Yuanyuan
    }

}
public class Student {

    private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

05_ The final keyword is used to decorate member variables

For a member variable, if the final keyword is used to modify it, the variable is still immutable.

  1. Because the member variable has a default value, it must be assigned manually after final is used, and the default value will not be given again.
  2. For the member variables of final, either direct assignment or constructor assignment is used. Choose one of the two.
  3. It must be ensured that all overloaded construction methods in the class will eventually assign values to the member variables of final.
public class Person {

    private final String name/* = "Lu Han“*/;

    public Person() {
        name = "Guan Xiaotong";
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

//    public void setName(String name) {
//        this.name = name;
//    }
}

06_ Four permission modifiers

There are four permission modifiers in Java:
public > protected > (default) > private
Same class (myself) yes yes yes
Same package (my neighbor) yes yes no
Different steamed stuffed buns (my son) Yes No
Different package non subclasses (strangers) Yes No

Note: (default) is not the keyword "default", but is not written at all.

public class Demo01Main {
}
public class MyClass {

    public int num = 10;

    public void method() {
        System.out.println(num);
    }

}
public class MyAnother {

    public void anotherMethod() {
//        System.out.println(new MyClass().num);
    }

}
package cn.itcast.day11.demo02.sub;

import cn.itcast.day11.demo02.MyClass;

public class MySon extends MyClass {


    public void methodSon() {
//        System.out.println(super.num);
    }

}
package cn.itcast.day11.demo02.sub;

import cn.itcast.day11.demo02.MyClass;

public class Stranger {

    public void methodStrange() {
        System.out.println(new MyClass().num);
    }

}

Keywords: Java Polymorphism

Added by grga on Tue, 04 Jan 2022 20:27:41 +0200