This is the 12th day of my participation in Gwen Challenge

abstract:

In this section we will examine the abstract keyword. Abstract modifiers can be used to modify classes and methods.

1.

Abstract modifiers make the class an abstract class that cannot generate object instances but can be declared as a type of object variable (that is, compile-time type). Abstract classes are like semi-finished classes that require subclasses to inherit and override their abstract methods.

package cn.hz;

/ * * *@author hz
 * @version1.0 * Defines an abstract class: Person */
public abstract class Person {
    private String name;  // Attribute: name

    // Non-abstract methods
    public void show(a){
        System.out.println("Human information."+name); }}Copy the code

Note: The abstract modifier must precede the class name when modifying a class.

2.

The abstract modifier makes the method abstract, meaning that it is declared (defined) but not implemented. The implementation part begins with “; “. Instead. Requires subclass inheritance implementations (overrides) such as abstract methods in abstract classes using the following code:

package cn.hz;

/ * * *@author hz
 * @version1.0 * Defines an abstract class: Person */
public abstract class Person {
    private String name;  // Attribute: name

    // Non-abstract methods
    public void show(a){
        System.out.println("Human information."+name);
    }
    
    // Abstract methods
    public abstract void eat(a);
}
Copy the code

Note: abstract in an abstract class cannot be omitted, whereas abstract in an interface can be omitted.

The code to use the abstract method in the interface is as follows:

package cn.hz;

/ * * *@author hz
 * @version1.0 * * Defines the data access layer interface for Person */
public abstract interface PersonDao {
    public abstract  Person find(a);
    public abstract  void add(a);
    public abstract  void update(a);
    public abstract  void delete(a);
}

Copy the code

Methods in interfaces can only be abstract methods, so when we write interfaces, we will directly omit the abstract on interfaces and methods, and the code is as follows:

package cn.hz;

/ * * *@author hz
 * @version1.0 * * Defines the data access layer interface for Person */
public  interface PersonDao {
    public  Person find(a);
    public  void add(a);
    public  void update(a);
    public  void delete(a);
}

Copy the code

Summary:

  • A class that is abstract may contain either abstract or non-abstract methods, but a class that contains abstract methods must be abstract.
  • Abstract methods can reside in interfaces and abstract classes, but need to be implemented by non-abstract subclasses or their implementation classes

final:

Abstract is abstract. In Java, there are occasions when classes, methods, variables, etc., are not allowed to change. Java provides the keyword final to ensure that the content does not change.

1. Final modifiers:

When final is used to modify a class, the class becomes final and cannot be inherited, as follows:

package cn.hz;

/ * * *@author hz
 * @version1.0 * Defines an abstract class: Person */
public final class Person {
    private String name;  // Attribute: name
    // Non-abstract methods
    public void show(a){
        System.out.println("Human information."+name); }}Copy the code

In Java, classes such as String and Integer are final and cannot be inherited. Be careful when using this class.

2. Final modification method:

A method modified by final must be nonabstract and cannot be overridden, but it can be inherited

package cn.hz;

/ * * *@author hz
 * @version1.0 * Defines an abstract class: Person */
public class Person {
    private String name;  // Attribute: name
    // This method is not allowed to be overridden
    public final void show(a){
        System.out.println("Human information."+name); }}Copy the code

Note that final decorators and classes do not affect each other.

3. The constants:

When fianl modifies a variable, the variable becomes a constant, which is not allowed to be changed later, and the constant needs to be assigned directly when it is defined as follows:

package cn.hz;

/ * * *@author hz
 * @version1.0 * defines a constant */
public class Person {
    public  final String NAME=Anonymous;  // Attribute: name
}

Copy the code

Constants are usually named in all uppercase, and in practice are defined as constants such as PI.

Summary:

  • Classes modified by final cannot be inherited, methods modified by final cannot be overridden, and variables modified by final cannot be changed as constants.

So much for the common keywords!