Translation, what is an interface!

1. What is an interface

We know that abstract classes can have ordinary methods or abstract methods. Subclasses that inherit abstract classes must implement the abstract methods of the parent class.

The interface is a special abstract class. Where is the special? In particular, it compares head iron and only sells abstract methods.

The interface is like some franchise stores. If you want to join, you must hang my signboard, and the decoration style must be according to mine.

2. Definition of interface

An interface is a reference data type.

The interface in java is defined using the interface keyword, for example:

[Modifier list] interface Interface name{}
// Define a student interface
public interface StudentService {
    
}

Ordinary classes and abstract classes can be inherited by subclasses, and the inherited keyword is extends. The interface has no inheritance, only "implementation."

Implementation is to implement the method declared in the interface. In other words, only one method is declared in the interface, and the implementation class of the interface must override this method.

Methods in the interface can only be implemented through classes. The keyword is implements, for example:

// Implementation class of StudentService interface
public class StudentServiceImpl implements StudentService{
    
}

3. Use of interface

3.1 variables in the interface must be initialized

Because the variables in the interface are of public static final type, the variables modified by final cannot be changed, so they must be initialized.

After modification:

// Define a student interface
public interface StudentService {
    String name ="Ha ha ha";
    public static void main(String[] args) {
        System.out.println(StudentService.name);
    }
}

Operation results:

3.2 all methods in the interface are public abstract by default and cannot be changed

That is, even if you do not write public abstract in front of the method, the interface will add these two modifiers to the method by default.

3.3 the interface cannot create an object

Interface is a special abstract class. It can only define abstract methods and variables, and cannot create objects or construct methods.

Maybe you should spray me at this time. When talking about abstract classes, didn't you say that abstract classes have construction methods? Even if an interface is a special abstract class, it is also an abstract class. Why is there no constructor?

As I said earlier when talking about abstract classes, the construction method of abstract classes is used to initialize properties when creating subclass objects.

However, the properties of the interface are of public static final type, which cannot be changed and have been initialized. Is it necessary to initialize properties with constructors?

Therefore, the interface cannot create objects and has no construction methods.

You may also refute me: "when talking about inheritance, didn't you first call the parent class constructor to create a subclass object? Without the constructor of the interface, the interface implementation class can't create an object?"

You can understand the implementation of the interface as inheritance, but the implementation interface does not inherit the parent class. Only inheritance can have subclasses, and the implementation of the interface method is called the interface implementation class. Therefore, the construction method of the interface is not required to create the interface implementation class.

Implementing an interface is mainly the method of implementing the interface declaration. What is the core? It's polymorphic.

3.4 the interface implementation class must implement all methods of the interface

Because the interface is a special abstract class, if a class implements the interface, it must implement all the abstract methods in the class, for example:

public interface StudentService {
    void add();
    void sub();
    void divide();
}
// Implementation class of StudentService interface
public class StudentServiceImpl implements StudentService{
    
    @Override
    public void add() {
        System.out.println("add");
    }
    @Override
    public void sub() {
        System.out.println("sub");
    }
    @Override
    public void divide() {
        System.out.println("divide");
    }
}

3.5 interfaces can be inherited, but interfaces cannot be implemented

Interface inheriting interface is actually for program expansion, but the implementation of interface must be implemented by class, for example:

// Teacher interface
public interface TeacherService {
    String school = "Leibus University";
}
// Define a student interface
public interface StudentService extends TeacherService {

    static void main(String[] args) {
        System.out.println(StudentService.school);
    }
}

Operation results:

Result analysis:

  1. Because the student interface inherits the teacher interface, the student interface also has the school attribute.
  2. Because all attributes in the interface are of public static final type, you can directly use the interface name Property name.
  3. Because all methods in the interface are modified by public abstract by default, the main method in the above example can remove public.

3.6 a class can implement multiple interfaces

In fact, multiple implementations of interfaces are equivalent to multiple inheritance, which solves the defect of single inheritance.

Implement multiple interfaces separated by commas. For example:

public interface TeacherService {
    String school = "Leibus University";
    void add();
}
// Define a student interface
public interface StudentService {
    void sub();
}
// Implementation class of StudentService interface
public class StudentServiceImpl implements StudentService,TeacherService{


    @Override
    public void sub() {
        
    }

    @Override
    public void add() {

    }
}

4. Difference between interface and abstract class

  • 1. Abstract classes can contain non abstract methods, and all the methods declared in the interface are abstract.
  • 2. Neither abstract classes nor interfaces can instantiate objects. The main function of abstract classes is to extract public code.
  • 3. The interface solves the problem of single inheritance

5. Understand the interface

In fact, our real life is full of examples of interfaces.

  • 1. The computer has a USB interface, which can be inserted into the keyboard, mouse, speaker, etc.
  • 2. You can charge your mobile phone with ordinary charger, fast charge and charging treasure.
  • 3. Bank tellers can serve customers all over the country.

These are all examples of interfaces. From the above, you can see the benefits of interfaces? Scalability.

In professional terms, interface oriented programming can reduce the coupling of programs and improve the scalability of programs.

Method callers only use interface oriented methods to call interface methods. The implementer of the interface implements the methods facing the interface.

The interface decouples the caller of the method from the implementer of the method. Isn't that polymorphism?

6. Interface cases

  1. New USB interface
// USB interface
public interface UsbService {

    // The equipment is working
    void work();
}
  1. New keyboard implementation class
// Keyboard implementation class
public class KeyboardServiceImpl implements UsbService {
    public static final String NAME = "keyboard";

    @Override
    public void work() {
        System.out.println("The keyboard is working");
    }
}
  1. New mouse implementation class
// Mouse implementation class
public class MouseServiceImpl implements UsbService {
    public static final String NAME = "mouse";

    @Override
    public void work() {
        System.out.println("The mouse is working");
    }
}
  1. New computer class
// Computer
public class Computer {
    // Connecting external devices
    public void connect(UsbService usbService){
        if(usbService instanceof KeyboardServiceImpl){
            System.out.println("The computer is connected"+KeyboardServiceImpl.NAME);
        }
        if (usbService instanceof MouseServiceImpl){
            System.out.println("The computer is connected"+MouseServiceImpl.NAME);
        }
        // Interface oriented programming, method callers face interface calls
        usbService.work();
    }
}
  1. Test class
public class InterfaceTest {
    public static void main(String[] args) {
        Computer computer = new Computer();
        UsbService usbService1 = new KeyboardServiceImpl();
        computer.connect(usbService1);
        System.out.println("----------------");
        UsbService usbService2 = new MouseServiceImpl();
        computer.connect(usbService2);
    }
}

Operation results:

Added by Jose Arce on Thu, 09 Dec 2021 12:20:56 +0200