Summary of basic knowledge points of Java II: abstract classes and interfaces

1: Abstract classes and interfaces
abstract class
1. Abstraction is to abstract the common characteristics of a class of entities. The parent class provides some attributes and behaviors for the child class, and the child class implements specific behaviors according to business requirements.
2. Abstract classes are decorated with abstract. Subclasses should implement all the abstract methods of their parent classes. Otherwise, subclasses are also abstract classes.
3. Abstract classes generally contain abstract methods, which must be located in abstract classes. The biggest difference between abstract classes and ordinary classes is that abstract classes cannot be instantiated. You need to inherit abstract classes to instantiate their subclasses.

public abstract class AbstractClassExample {

    protected int x;
    private int y;

    public abstract void func1();

    public void func2() {
public class AbstractExtendClassExample extends AbstractClassExample {
    public void func1() {
// AbstractClassExample ac1 = new AbstractClassExample(); // 'AbstractClassExample' is abstract; cannot be instantiated
AbstractClassExample ac2 = new AbstractExtendClassExample();

Interface is an extension of abstract class. Before Java 8, it can be regarded as a completely abstract class, that is to say, it cannot have any method implementation. Starting from Java 8, the interface can also have the default method implementation, because the maintenance cost of the interface that does not support the default method is too high. Before Java 8, if an interface wanted to add new methods, it was necessary to modify all classes that implemented the interface. Interface members (fields + methods) are public by default, and cannot be defined as private or protected. The fields of the interface are static and final by default

public interface InterfaceExample {
    void func1();

    default void func2(){

    int x = 123;
    // int y;               // Variable 'y' might not have been initialized
    public int z = 0;       // Modifier 'public' is redundant for interface fields
    // private int k = 0;   // Modifier 'private' not allowed here
    // protected int l = 0; // Modifier 'protected' not allowed here
    // private void fun3(); // Modifier 'private' not allowed here
public class InterfaceImplementExample implements InterfaceExample {
    public void func1() {
// InterfaceExample ie1 = new InterfaceExample(); // 'InterfaceExample' is abstract; cannot be instantiated
InterfaceExample ie2 = new InterfaceImplementExample();

From the design level, the abstract class provides an IS-A relationship, so it must meet the internal substitution principle, that is, the subclass object must be able to replace all the parent objects. The interface is more like a LIKE-A relationship. It only provides a method to implement the contract, and does not require the interface and the class implementing the interface to have an IS-A relationship.
In terms of use, a class can implement multiple interfaces, but cannot inherit multiple abstract classes. The fields of the interface can only be static and final, while the fields of the abstract class have no such restrictions. Interface members can only be public, while members of abstract classes can have multiple access rights.

Use selection

Interface used:
1. You need to make irrelevant classes implement a method;
2. Multiple inheritance is required.
Use abstract classes:
1. You need to share code in several related classes.
2. You need to be able to control the access rights of inherited members, instead of all being public.
3. You need to inherit non static and non constant fields.

In many cases, the interface takes precedence over the abstract class, because the interface has no strict class hierarchy requirements of the abstract class, and can flexibly add behavior to a class. Starting from Java 8, the interface can also be implemented by default, which makes the cost of modifying the interface very low. Moreover, to meet the principle of composite reuse, we should try to use combination or aggregation relationship to realize code reuse, use less inheritance and reduce code coupling.

1. Accessing the constructor of the parent class: you can use the super() function to access the constructor of the parent class, so as to delegate the parent class to complete some initialization work.
2. Accessing members of the parent class: if a subclass overrides the implementation of a method in the parent class, you can use the super keyword to reference the implementation of the parent class's method.

Rewriting and overloading

1. Override
In the inheritance system, the subclass implements a method that is exactly the same as the parent class in method declaration.
In order to meet the principle of Li formula substitution, rewriting has the following two limitations:

  • The access permission of the subclass method must be greater than or equal to that of the parent method;
  • The return type of a subclass method must be the return type of a parent method or its subtype.

Check whether the above two conditions are met by @ Override.

2. Overload
In the same class, a method has the same name as an existing method, but at least one parameter type, number and order are different. It should be noted that if the return value is different, the others are the same and are not overloaded.

Keywords: Java JavaEE

Added by lol on Wed, 09 Mar 2022 10:03:03 +0200