Java learning road-interface

Java learning road-interface

Overview

As we all know, we are the children of our parents. We have inherited both our father's genes and our mother's genes. This is multiple inheritance.

However, in Java programs, multiple inheritance is not supported. Java only supports single inheritance. But the interface provides us with a possibility to implement multiple inheritance!

Interface (English: Interface): In the JAVA programming language interface is an abstract type, is a collection of abstract methods, interfaces usually interfacedeclared. A class inherits the abstract methods of the interface by inheriting the interface.

We have to make it clear that **interfaces are not classes! Interface and class are side by side structure! **It's just that the way to write interfaces is very similar to classes, but they belong to different concepts. The class describes the properties and methods of the object. The interface contains the methods to be implemented by the class.

Unless the class implementing the interface is an abstract class, the class must define all methods in the interface.

The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class. In addition, in Java, interface types can be used to declare a variable, they can become a null pointer, or be bound to an object implemented by this interface.

1. The definition and implementation of the interface

Before JDK7, only global constants and abstract methods can be defined in interfaces; after JDK8, in addition to global constants and abstract methods, static methods, default methods, etc. can also be defined in interfaces.

The class that implements the interface must implement all the abstract methods of the interface, while static methods and default methods do not need to be implemented. Static methods and default methods in the interface can only be called through the interface.

// 定义一个接口
interface Flyable {
    
    
    // 全局常量
    // 两种方式效果相同,一般书写时省略前面的 public static final
    public static final int MAX_SPEED = 7900;
    int MIN_SPEED = 0;

    // 抽象方法
    // 两种方式效果相同,一般书写时省略前面的 public abstract
    public abstract void fly();
    void stop();
}

class Bird implements Flyable {
    
    
    @Override
    public void fly() {
    
    
        System.out.println("I can fly.");
    }

    @Override
    public void stop() {
    
    
    }
}

Second, the inheritance of the interface

One interface can inherit another interface, which is similar to the way of inheritance between classes. The inheritance of the interface uses the extends keyword, and the child interface inherits the method of the parent interface.

The implemented class must implement all interfaces (including interfaces inherited by interfaces) before it can be instantiated.

Single inheritance

// 定义一个接口
interface Flyable {
    
    
    public abstract void fly();
    void stop();
}

// 继承一个接口
interface Bird extends Flyable {
    
    
    void eatBug();
}

// 实现接口
class Sparrow implements Bird {
    
    
    @Override
    public void fly() {
    
    
    }

    @Override
    public void stop() {
    
    
    }

    @Override
    public void eatBug() {
    
    
    }
}

Multiple inheritance

In Java, multiple inheritance of classes is illegal, but interfaces allow multiple inheritance. In the multiple inheritance of the interface, the extends keyword only needs to be used once, followed by the inherited interface to achieve multiple inheritance.

Through multiple inheritance of interfaces, multiple inheritance of classes can be realized.

// 定义一个接口
interface Flyable {
    
    
    void fly();
}

interface Reproduction {
    
    
    void breedingOffspring();
}

// 继承一个接口
interface Bird extends Flyable, Reproduction {
    
    
    void eatBug();
}

// 实现接口
class Sparrow implements Bird {
    
    
    @Override
    public void fly() {
    
    
    }

    @Override
    public void eatBug() {
    
    
    }

    @Override
    public void breedingOffspring() {
    
    
    }
}

Three, mark interface

Tag interface is sometimes called tag interface (Tag interface), that is, the interface does not contain any methods.

Examples are easy to find tagged interface in Java, such as the JDK Serializablethe interface is a marker interface.

A marked interface is an interface without any methods and attributes. It only indicates that its class belongs to a specific type, for other codes to test and allow to do something.

Marking interface function: In simple terms, it is to mark an object (stamp) so that the object has certain privileges or privileges.

public interface Flyable {
    
    
    
}

Fourth, the comparison of interfaces and classes

  • Interface cannot be used to instantiate objects, objects can;
  • Interface has no constructor, class has constructor
  • All methods in the interface must be abstract methods, and all methods in the class cannot be abstract methods;
  • The interface cannot contain member variables, except for static and final variables, the class can have member variables;
  • The interface is not inherited by the class, but to be implemented by the class;
  • The interface supports multiple inheritance, and the class only supports single inheritance.

Five, interface default method conflict

If you first define a method as a default method in an interface, and then define the same method in a super class or another interface, an ambiguity error will occur.

To solve this problem, Java has the following rules.

  1. **Super class first: **If the super class provides a specific method, the default method with the same name and the same parameters will be ignored;
  2. **Interface conflict:** If one interface provides a default method, another interface provides a method with the same name and the same parameter type. The implementation class must override this method to resolve the conflict.

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112636740