Interface in Java

Basic introduction

Interface is to give methods that are not implemented at one time, package them together, and write these methods according to the specific situation when a class is to be used.

Syntax: interface interface name{

Attribute + method

}

class name implements interface{

Own properties / / methods

}

Note: before jdk7, there were no methods in the interface and no method body. After 8.0, there can be static methods and default methods (which need to be marked with default), that is, there are specific implementations of methods in the interface, and abstract methods in the interface can be omitted.

public interface InterfaxeTest {
    public int a=10;
    public int sum();
   default public void hi(){
        System.out.println("Hello");
    }
    public static void ok(){
       System.out.println("OK");
    }
}

Simple interface usage

  • Standardize our writing and facilitate calling. For example, we now need to write three classes to link mysql database and oracle database respectively. The linking method of mysql database written by programmer 1 is defined as f1 and disconnection as f2. The method of linking oracle database written by programmer 2 is connect and close. This will lead to the trouble of calling functions when we use different databases, but when we use interfaces, we can standardize the naming of each one, for example:
public interface DBInterface {
    /**Define link method*/
    public void connect();
    /**Define closing method*/
    public void stop();
}
public class Oracle implements DBInterface{
    @Override
    public void connect() {
        System.out.println("link oracle database");
    }

    @Override
    public void stop() {
        System.out.println("break link oracle database");
    }
}
public class MySql implements DBInterface{
    @Override
    public void connect() {
        System.out.println("link mysql database");
    }

    @Override
    public void stop() {
        System.out.println("break link mysql database");
    }
}
public class Test {
    public static void main(String[] args) {
        MySql mySql = new MySql();
        mySql.connect();
        mySql.stop();
    }

}

Precautions for interface

  • The interface itself cannot be instantiated
  • All methods in the interface are public methods. Abstract methods in the interface can be modified without abstract
  • If a class inherits the interface, it must implement all methods of the interface
  • A class can implement multiple interfaces
public class Oracle implements DBInterface,InterfaxeTest{
    @Override
    public void connect() {
        System.out.println("link oracle database");
    }

    @Override
    public void stop() {
        System.out.println("break link oracle database");
    }

    @Override
    public int sum() {
        return 0;
    }

    @Override
    public void hi() {
        InterfaxeTest.super.hi();
    }
}
  • The attribute in the interface can only be final, and it is a public static final modifier, such as int # a=1; In fact, it is public static int a=1;

prove:

We defined int a=0 in the interface just now;

In the main method, the output is as follows; If there is no error and the output is normal, it means that this is public and static, and we have not created an instantiated object. final only needs us to try to change the following values.

public class Test {
    public static void main(String[] args) {
        System.out.println(MySql.a);
        
    }

}

Do an exercise:

public class Test {
    public static void main(String[] args) {
        System.out.println(DBInterface.a);
        System.out.println(MySql.a);
        MySql mySql=new MySql();
        System.out.println(mySql.a);
    }
}

 

Keywords: Java Back-end interface

Added by donnierivera on Tue, 08 Feb 2022 09:05:25 +0200