Simple understanding of "Abstract abstract class" and "Interface interface" in Java“

Posted by blacksnday on Sun, 20 Feb 2022 14:21:25 +0100

First of all, we need to know that "Abstract class" and "Interface" are two mechanisms that support the definition of Abstract classes

1. Abstract abstract class

So what is an abstract class in Java?

In Java, due to polymorphism, a class can be inherited by multiple subclasses as a parent class. For example, the Student subclass and Teacher subclass derived from the Person parent class can override the run() method.

class Person {
    public void run() { ... }
}
class Student extends Person {
    @Override
    public void run() { ... }
}
class Teacher extends Person {
    @Override
    public void run() { ... }
}

If the run() method of the parent class Person does not have the code logic to be executed, can the execution statement in the method body be removed? The answer is no, because it will lead to compilation errors, because when defining a method, the statement of the method must be implemented. Can you remove the run() method of the parent class? The answer is still no, because removing the run() method of the parent class will lose its polymorphism. For example, if runTwice() is defined before, it cannot be compiled!

Therefore, if the method of the parent class itself does not need to implement any function, just to define the method signature and let the subclass override it, then the method of the parent class can be declared as an abstract method: a method declared as abstract indicates that it is an abstract method and cannot have a method body. Because the abstract method itself cannot be executed, the Person class at this time is also regarded as an abstract class, and the abstract class cannot be instantiated. The compiler will tell us that the Person class cannot be compiled because it contains abstract methods.

From the code:

Error:

class Person { //The compiler will report an error
 /*Abstract method 1. No method body
  *         2,Add the abstract keyword between the modifier and the return value
  *         3,Must exist in an abstract class with abstract
	              */
 public abstract void run(); 
}

Correct:

abstract class Person {
 /*Abstract method 1. No method body
  *         2,Add the abstract keyword between the modifier and the return value
  *         3,Must exist in an abstract class with abstract
	              */
//Abstract method 1
 public abstract void run(); 
//Abstract method 2
 public abstract void say(); 
}

Summary:

If a method is defined in a class, the method has no specific execution code (method body). Then, this method is an abstract method, which is decorated with abstract. Because abstract methods cannot be executed, this class must also be defined as an abstract class. It can also be understood as: abstract methods must be defined in abstract class. (Note: abstract methods do not necessarily exist in abstract classes)

2. Interface interface

In abstract classes, abstract methods essentially define behavior specifications: specify the abstract behavior (abstract method) in the parent class, and require all subclasses to implement the abstract method. Thus, the constraint of subclass behavior specification is realized. It is mainly used to realize polymorphism. However, if an abstract class has no fields (member variables) and all methods are abstract methods, the abstract class can be defined as interface interface. In Java, use the interface keyword to declare an interface:

interface Person {
    void run();
    String getName();
}

The so-called interface is a pure abstract code structure that is more abstract than an abstract class. Because it can't even exist fields (member variables), it can only contain constants, abstract methods, default methods, etc. In addition, all methods defined by the interface are public abstract by default, so these two modifiers do not need to be defined (the effect of writing or not writing is the same).

When a specific class implements an interface, you need to use the implements keyword. All subclasses are required to implement the abstract method. For example:

class Student implements Person {
    private String name;

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

    @Override
    public void run() {
        System.out.println(this.name + " run");
    }

    @Override
    public String getName() {
        return this.name;
    }
}

In Java, a class can only extend from another class (single inheritance). However, a class can implement multiple interface interfaces (multiple implementations). For example:

// Two interface s are implemented
class Student implements Person, Hello { 
    ...
}

In addition, the interface can define the default method (JDK version > = 1.8), and the default method can contain the method body. The default method is different from the ordinary method of the abstract class. Because the interface has no field, the default method cannot access the field, while the ordinary method of the abstract class can access the instance field.

The implementation class does not have to override the default method. The purpose of the default method is that when we need to add a method to the interface, it will involve modifying all subclasses. If you add a default method, you don't have to modify all the subclasses. You just need to override the new method where you need to override it. For example, change the run() method of the Person interface to the default method:

public class Main {
    public static void main(String[] args) {
        Person p = new Student("Xiao Ming");
        p.run();
    }
}

interface Person {
    String getName();
    default void run() {
        System.out.println(getName() + " run");
    }
}

class Student implements Person {
    private String name;

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

    public String getName() {
        return this.name;
    }
}

Topics: Java Back-end