Summary of interface knowledge in Java

  • 2020-05-09 18:32:33
  • OfStack

1. Why use interfaces

  suppose there is a requirement: the requirement to achieve the function of the security door. The door has the function of "open" and "close", and the lock has the function of "lock" and "unlock".

Analysis: first, guard against theft is a door, the door is open and close function, one lock, lock and unlock a lock, in accordance with the thought of object-oriented programming, and lock the door we will exist alone as a class, but can't keep the door of inherited from at the same time, inherited from the lock of the door, security door is not locked, do not conform to the inheritance of is a relations, in java support single inheritance. So how do we solve this 1 problem, this is where the interface comes in.

      2. What is an interface

      in software interface is 1 kind of norms and standards, they can constraint the behavior of the class, is 1 method characteristic collection, but there is no way that the implementation of the interface is also can be seen as a special abstract class, but with completely different ways to represent an abstract class, and the design concept is also different, abstract class promotes code reuse, interface is conducive to the expansion of the code and maintain.

3. Differences between abstract classes and interfaces:

The abstract class can provide the implementation details of the member method, while the public abstract method can only exist in the interface.

02. Member variables in abstract classes can be of various types, while member variables in interfaces can only be of type public static final;

Interfaces cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods.

04.1 classes can only inherit one abstract class, while one class can implement multiple interfaces.

4. How do you define an interface

  starts with the syntax 1:

    [modifier] interface interface name extends parent interface 1, parent interface 2...

    {

        // constant definition

        // method definition

    }

    implements the interface syntax in one class:

class class name superclass name implements interface 1, interface 2...

{

  // class member

}

5. Define interface considerations

    01. The interface has the same naming rules as the class. If the modifier is public, the interface is visible throughout the project. If you omit the modifier, the interface is only visible in the current package.

    02. You can define constants in the interface, but you can't define variables. If you define a property in the interface, you can decomcompile it and see that it is automatically decorated with public static final.

      03. All methods in the interface are abstract methods, and methods in the interface are automatically decorated with public abstract, which means that there are only global abstract methods in the interface.

      04. The interface cannot be instantiated and there cannot be constructs in the interface.

      05 interfaces can be inherited through extends. One interface can inherit multiple interfaces, but interfaces cannot inherit classes.

      06. The implementation class of the interface must implement all the methods of the interface, otherwise it must be defined as an abstract class.


Related articles: