Abstract class, interface

Article directory

1. abstract

What's in an abstract class?

  • Abstract classes are mostly the same as ordinary classes. They can have properties, methods, static members, be inherited, or inherit other classes.

  • The biggest difference between abstract classes and ordinary classes is that there can be abstract methods in abstract classes. Abstract methods only have method declaration, no method body.
    eg: public int abstractfoundation(); / / abstract method has no cube

  • Note 1: abstract classes cannot instantiate objects.

  • Note 2: a class inherits from an abstract class. If all the abstract methods in the parent class are not implemented, the class should continue to be defined as an abstract class.

Why do abstract classes exist?

  • From a design point of view, some classes should not be instantiated, so they can be designed as abstract classes.
  • Abstract classes and abstract methods exist to be inherited by other classes and override their abstract methods. When using abstract classes, it is closely related to inheritance and polymorphism syntax.
  • Abstract class is a syntax requirement and a checking mechanism, in order to reduce the probability of code error.

Explanation: an abstract class, without abstract, becomes a normal class, and the code can also run, with no difference in results. In addition, abstract has more syntax validation to prevent inadvertently creating objects in the code that should not be instantiated.

2. interface

What's in the interface?

Note: Methods in the interface default to public abstract.

Why do interfaces exist?

  • From the design point of view, the significance of interface is to break through the limitation of single inheritance.
  • Interfaces are a little more strict than abstract classes.

Note: interfaces can also inherit multiple interfaces.

public interface interface extensions interface 1, interface 2 {}

Instance? Compatible interface

package sort;

public class Boy implements Comparable<Boy> {
    private String name;
    private int money;
    private int faceValue;

    public Boy(String name, int money, int faceValue) {
        this.name = name;
        this.money = money;
        this.faceValue = faceValue;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "name='" + name + '\'' +
                ", money=" + money +
                ", faceValue=" + faceValue +
                '}'+"\r\n";
    }

    @Override
    public int compareTo(Boy o) {
        // Compare this with other
        // If this is in front of other, it should return < 0
        // If this is after the other, you should return > 0
        // If this is tied to other, it should return 0
        if (this.money > o.money) {
            // this in front of
            return -1;
        }
        if (this.money < o.money) {
            // other in front of
            return 1;
        }
        // More money, more face
        if (this.faceValue > o.faceValue) {
            return -1;
        }
        if (this.faceValue < o.faceValue) {
            return 1;
        }
        // If money and face are the same, they are tied
        return 0;
    }
}
package sort;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Boy[] boys = {
                new Boy("Daniel Wu", 100, 100),
                new Boy("Xiao Zhan", 50, 80),
                new Boy("Hu Ge", 100, 120),
                new Boy("Troll Warrior", 20, 20)
        };

        // Compilation error, 1
        // Compile passed, run exception 2
        // Run through, can sort out a result 3
        sort(boys);
        System.out.println(Arrays.toString(boys));
    }

    // The simulation implements the following Arrays.sort
    // Compatible originally has a generic parameter. This parameter can also be absent. If not, it means Object
    public static void sort(Comparable[] array) {
        for (int bound = 0; bound < array.length; bound++) {
            for (int cur = array.length - 1; cur > bound; cur--) {
                if (array[cur - 1].compareTo(array[cur]) > 0) {
                    // If the former element does not meet the sorting requirements compared with the latter, exchange two elements
                    Comparable tmp = array[cur - 1];
                    array[cur - 1] = array[cur];
                    array[cur] = tmp;
                }
            }
        }
    }
}

3. Difference between interface and abstract class

Common ground: reflects polymorphism.

  • The difference is 1:
    Abstract classes can contain ordinary methods and fields, which can be directly used by subclasses (no need to override).
    Interface can only contain abstract methods and static constants (public static final by default), not ordinary methods. Subclasses must override all abstract methods.
  • The difference is 2:
    Abstract classes follow the rule of "single inheritance". A class cannot inherit from multiple ordinary classes.
    Interface follows the rule of "multiple inheritance". A class can implement multiple interfaces at the same time.
    Note: public class bird extends air implements iflying, iruning {}
  • The difference is 3:
    At the design level, abstract class is the abstract of behavior, and interface is the abstract of action.
  • The difference is 4:
    Abstract methods can only be declared and cannot be implemented. Abstract classes are the result of refactoring and interfaces are the result of design.
Difference abstract class interface (interface)
Structure composition Common class + abstract method Abstract method + global 1 constant
Jurisdiction Various powers public
Subclass usage Using the extends keyword to inherit abstract classes Using the implements keyword to implement the interface
relationship An abstract class can implement several interfaces Interfaces cannot inherit abstract classes, but interfaces can use the extends keyword to inherit multiple parent interfaces
Subclass privilege A subclass can only inherit one abstract class A subclass can implement multiple parent interfaces
Published 106 original articles, won praise 6, visitors 2949
Private letter follow

Tags: Java

Posted on Sat, 07 Mar 2020 19:24:53 -0800 by hodge