Java notes - Interface

Posted by Chafikb on Sun, 21 Nov 2021 05:58:23 +0100

Interface

An interface is a declaration of a series of methods and a collection of method features. An interface has only method features and no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions).

Why do I need an interface

The class is not completely abstract, relative to the object is abstract, and the method of the class is concrete. Class can only abstract properties, not behavior. The methods in the interface are completely abstract. Using classes and interfaces can realize the abstraction of attributes and behavior.

grammar

Basic grammar

The attributes in the interface are modified by static final by default and need to be initialized

The abstract method in the interface has only declaration but no implementation

public interface InterfaceTest{
    static final String str = "...";
    
    public void function();
}
public class TestClass implements InterfaceTest{
    public void function(){
        //Implementation of method
    }
}

Default method

The default is modified as the default method in the interface. The default method needs to be implemented in the interface. The class that implements the interface can not rewrite the default method or rewrite the default method. After rewriting, the method after rewriting is called.

public interface InterfaceTest {
    default void function1(){
        System.out.println("Method 1");
    }

    default void function2(){
        System.out.println("Method 2");
    }
}
public class TestClass implements InterfaceTest {
    @Override
    public void function1(){
        System.out.println("Override method 1");
    }
}
public class test {
    public static void main(String[] args) {
        TestClass testClass = new TestClass();
        testClass.function1();
        testClass.function2();
    }
}

Operation results

Override method 1
 Method 2

When a class implements multiple interfaces and these interfaces have the same default method, the class must override the default method

Static method

The static modified method in the interface is a static method. Like the static method in the class, it does not belong to an object. The static method will not be implemented by other classes by calling InterfaceName.function()

public interface InterfaceTest {
    public static void staticFunction(){
        System.out.println("This is a static method");
    }
}
public class test {
    public static void main(String[] args) {
        InterfaceTest.staticFunction();
    }
}

Operation results

This is a static method

static final

static unique

  • Property: it does not belong to an object and is called by the class name
  • Method: not an object, called by the class name
  • Class: only inner classes can be modified by static
  • Code block: called when the class is loaded, independent of creating the object

Final final form is immutable

  • Attribute: constant; cannot be modified later
  • Method: cannot be overridden
  • Class: cannot be inherited, such as String

Interface application instance

Take the door as an example, the door needs to be equipped with a lock, and the door can be opened and closed through the switch of the lock. But there are many kinds of locks. A door can be equipped with different kinds of locks, such as latch, password lock, etc.

First, define a lock interface

public interface Lock {
    void open();
    void close();
}

Then define a door that needs a lock

public class Door {
    private Lock lock;

    public void setLock(Lock lock){
        this.lock = lock;
    }

    public void openDoor(){
        lock.open();
    }

    public void closeDoor(){
        lock.close();
    }
}

Then define different types of locks to implement interfaces and pass them to the door

public class FaceLock implements Lock{
    @Override
    public void open() {
        System.out.println("Face recognition successful, open the door");
    }

    @Override
    public void close() {
        System.out.println("Face recognition successful, close the door");
    }
}

test method

public class test {
    public static void main(String[] args) {
        Door door = new Door();
        FaceLock faceLock = new FaceLock();
        //Install a lock on the door
        door.setLock(faceLock);
        
        door.openDoor();
        door.closeDoor();
    }
}

Output results

Face recognition successful, open the door
 Face recognition successful, close the door

You can also define another password lock

public class PasswordLock implements Lock {
    @Override
    public void open() {
        System.out.println("The password is correct. Open the door");
    }

    @Override
    public void close() {
        System.out.println("The password is correct. Close the door");
    }
}

Retest

public class test {
    public static void main(String[] args) {
        Door door = new Door();
        FaceLock faceLock = new FaceLock();
        //Install a face recognition lock on the door
        door.setLock(faceLock);

        door.openDoor();
        door.closeDoor();

        PasswordLock passwordLock = new PasswordLock();
        //Install a code lock on the door
        door.setLock(passwordLock);
        door.openDoor();
        door.closeDoor();
    }
}

Output results

Face recognition successful, open the door
 Face recognition successful, close the door
 The password is correct. Open the door
 The password is correct. Close the door

Using the interface, you can realize different ways of opening the door, that is, you can realize the behavior abstraction of the door. Using the interface reasonably in the process of software development can improve the scalability.

Topics: Java interface