Mind map:
4.2.1 final keyword modified class
Definition and basic concepts: In Java, the final
keyword has the meaning of “final” or “immutable”. Elements (classes, methods or variables) modified with the final
keyword have specific characteristics.
Main applications and precautions:
- Modified class: Classes modified with
final
cannot be inherited. - Modified method: Methods modified with
final
cannot be overridden by subclasses. - Modified variables: Variables modified with
final
become constants and cannot be modified.
4.2.1 final
Keyword modified class
In Java, when a class is modified with the final
keyword, other classes cannot inherit this class, that is, subclasses are not allowed.
Example:
File name: Example07.java
// Use the final keyword to modify the Animal class final class Animal { // Other members of the class... } //The Dog class tries to inherit from the Animal class class Dog extends Animal { // This will cause a compilation error } //Define test class public class Example07 { public static void main(String[] args) { Dog dog = new Dog(); // Create an object of Dog class } }
In the above code, the Animal class is modified with the final
keyword, but the Dog class tries to inherit it. This resulted in a compilation error: “Cannot inherit from final com.itheima.Animal”.
Conclusion: The final
keyword ensures that a class will not be inherited. This is an important protection mechanism for classes and can be used when you do not want a class to be inherited by other classes.
Through the above content, we can clearly understand the role and importance of the final
keyword in Java programming. When you want an element (class, method or variable) not to be modified or inherited, you should use the final
keyword for modification.
4.2.2 final
Keyword modification method
Definitions and basic concepts:
In Java, when a method is modified with the final
keyword, the method cannot be overridden by subclasses.
Example description:
File name: Example08.java
//Define Animal class class Animal { // Use the final keyword to modify the shout() method public final void shout() { //Method implementation... } } //Define the Dog class and try to inherit the Animal class class Dog extends Animal { // Try to override the shout() method of the Animal class public void shout() { // This will cause a compilation error } } //Define test class public class Example08 { public static void main(String[] args) { Dog dog = new Dog(); // Create an object of Dog class } }
In the above code, the shout()
method of the Animal
class is modified by the final
keyword, but the Dog
class tries Override this method. This resulted in a compilation error: “shout() in com.itheima.Dog cannot override shout() in com.itheima.Animal, the overridden method is final”.
Main conclusions:
- Methods modified with
final
cannot be overridden by subclasses. - If you try to do this, the compiler will complain.
- This is a protection mechanism that ensures that the implementation of a method is not changed, especially when you believe that the current implementation of the method is optimal or do not want it to be overridden for security reasons.
Summary: The final
keyword provides a way for Java developers to ensure that methods will not be changed or overridden in subclasses. Use this keyword when you want to fix the behavior of a method and prevent any potential modifications.
4.2.3 final
Keyword modified variable
Definition and basic concepts: In Java, when a variable is modified with the final
keyword, the variable becomes a constant. A constant can only be assigned a value once, and assignment can only occur when it is declared. The value of the constant cannot be changed in subsequent programs.
Example description:
File name: Example09.java
public class Example09 { public static void main(String[] args) { final int AGE = 18; // The variable AGE modified with the final keyword is assigned an initial value of 18 AGE = 20; // Try to reassign the constant AGE, which will cause a compilation error } }
Compile this code, the compiler will report an error: “Cannot assign a value to the final variable AGE”. This means that attempts to reassign a final
modified constant are not allowed.
Main conclusions:
- Variables modified with
final
are constants and can be assigned a value only once. - Attempting to reassign a variable modified with
final
will result in a compile-time error. - General convention: Variable names declared using
final
should be in all capital letters.- For example:
public static final String NAME = "Husky";
- For example:
- If a variable is declared using
public static final
, the variable will be treated as a global constant.
Summary: The final
keyword provides a way for Java developers to ensure that the value of a variable is not modified during its life cycle. This is useful in many situations, such as when you want to define a configuration value or global constant that should not be changed.
Summary:
4.2 final
Keyword summary
Key points:
- Definition:
final
is a keyword in Java, which means “final” and “unchangeable”. - Application scenarios:
- Modify class: Make the class non-inheritable.
- Modify method: Make the method not overridable.
- Modify variable: Make the variable a constant and can only be assigned a value once.
Difficulties:
- Understanding and Distinguishing: Newbies may be confused about the different meanings and effects of
final
modified classes, methods and variables. Especially when used in combination with other modifiers such asstatic
,abstract
, etc. - Practical Application: Determining when to use
final
to modify a class, method, or variable requires a deep understanding of Java inheritance and polymorphism.
Error-prone points:
- Repeated assignment: Attempting to assign a value to a variable modified by
final
more than once will result in a compilation error. - Misuse of inheritance: Attempting to inherit from a class modified by
final
will result in compilation errors. - Override Error: Attempting to override a method modified by
final
will also result in a compilation error. - Naming convention: The constants modified by
final
should be all capitalized according to Java naming convention, and may be forgotten. - Misunderstanding Scope: You may mistakenly believe that the
final
keyword can affect the scope of a variable, but in fact it only affects the modification permission of the variable.
In short, the final
keyword provides a mechanism in Java to ensure that certain content (classes, methods, or variables) will not be modified. In order to take full advantage of this feature and avoid related errors, developers need to have a deep understanding of it.
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java skill treeBasic syntax of JavaKeywords in Java 138777 people are learning the system