Java basic syntax 69- interface

Posted by aladiyat23 on Thu, 14 Oct 2021 22:23:43 +0200

Java basic syntax 69- interface

1, Use of interfaces

  1. Interfaces are defined using interface s
  2. In java, interfaces and classes are parallel structures
  3. How to define an interface: define members in an interface

3.1 JDK7 and before: only global constants and abstract methods can be defined

Global constant: public static final, but it can be omitted when writing

Abstract method: public abstract

3.2 JDK8: in addition to defining global constants and abstractions, you can also define static methods and default methods

  1. Constructor cannot be defined in interface! This means that this interface cannot be instantiated
  2. java development, let the class to implement (implements) to use. If the implementation class covers the abstract methods in the interface, the implementation class can be instantiated. If it does not cover all the abstract methods, the implementation class is an abstract class
  3. Java classes can implement multiple interfaces -- > making up for the limitation of single inheritance

Format: Class AA extensions BB implementations CC, DD, EE

  1. Interfaces can inherit from one interface to another, and multiple interfaces can inherit
package com.java11;

public class InterfaceTest {
    public static void main(String[] args) {
        System.out.println(Flyable.MAX_SPEED);
        System.out.println(Flyable.MIN_SPEED);
    }
}

interface Attackable{
    public abstract void attack();
}

interface Flyable{
    //Global constant
    public static final int MAX_SPEED = 7900;
    public static final int MIN_SPEED = 1;

    //Abstract method
    public abstract void  fly();
    //public abstract is omitted
    void stop();

}

class Plane implements Flyable{

    @Override
    public void fly() {
        System.out.println("Take off through the engine!");
    }

    @Override
    public void stop() {

    }
}

abstract class Kite implements Flyable{

}

class Bullet extends Object implements Flyable,Attackable,CC{

    @Override
    public void attack() {

    }

    @Override
    public void fly() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void AA() {
        
    }

    @Override
    public void BB() {

    }
}

interface AA{
    public abstract void AA();
}

interface BB{
    public abstract void BB();
}

interface CC extends AA,BB{

}
  1. The use of interfaces reflects polymorphism
  2. Interface can actually be regarded as a usage specification
package com.java11;

public class USBTest {
    public static void main(String[] args) {
        Computer computer = new Computer();
        Flash flash = new Flash();
        computer.transferData(flash);
    }
}

class Computer{
    public void transferData(USB usb){
        usb.start();
        System.out.println("Specific transmission details");
        usb.stop();
    }
}

interface USB{
    void start();
    void stop();

}

class Flash implements USB{

    @Override
    public void start() {
        System.out.println("U Disc opening operation");
    }

    @Override
    public void stop() {
        System.out.println("U Disk end work");
    }
}

class Printer implements USB{
    @Override
    public void start() {
        System.out.println("Printer on");
    }

    @Override
    public void stop() {
        System.out.println("Printer finished working");
    }
}

2, Application of interface: proxy mode

package com.java11;

public class NetWorkTest {
    public static void main(String[] args) {
        Sever sever = new Sever();
        ProxySever proxySever = new ProxySever(sever);
        proxySever.browse();
    }
}

interface NetWork {
    public void browse();
}

//Proxy class
class Sever implements NetWork {

    @Override
    public void browse() {
        System.out.println("Real servers to access the network");
    }
}

//proxy class
class ProxySever implements NetWork {

    public NetWork work;

    public ProxySever(NetWork work) {
        this.work = work;
    }

    public void check() {
        System.out.println("Pre Internet inspection");
    }

    @Override
    public void browse() {
        check();
        work.browse();
    }
}

3, New features of Java 8 interface

JDK8: in addition to defining global constants and abstractions, you can also define static methods and default methods

Knowledge point 1: static methods defined in the interface can only be called through the interface.

Knowledge point 2: by implementing the object of the class, you can call the default method in the interface. If the implementation class overrides the default method in the interface, the overridden method will still be called Class priority principle

Knowledge point 3: if a subclass or implementation class declares a method with the same name and parameter in the inherited parent class and the implemented interface, the subclass will call the method with the same name and parameter in the parent class by default without overriding this method

Knowledge point 4: if multiple interfaces are implemented in the implementation class, the interface defines methods with the same name and parameters. If the method is not rewritten, an error is reported - Interface conflict

This makes it necessary to override the methods in the interface

Knowledge point 5: how to call the parent class in the subclass or implementation class method and the method to be rewritten in the interface.

package com.java11;

public interface CompareA {
    public static void method1(){
        System.out.println("CompareA: Beijing");
    }

    public default void menthod2(){
        System.out.println("CompareA:Shanghai");
    }

    default void method3(){
        System.out.println("CompareA:Shanghai");

    }

}
package com.java11;

public interface CompareB {
    default void method3(){
        System.out.println("CompareB:Shanghai");

    }
}
package com.java11;

public class SuperClass {
    public void method3(){
        System.out.println("SuperClass:Beijing");
    }
}
package com.java11;

public class SubClassTest {
    public static void main(String[] args) {
        SubClass subClass = new SubClass();
        subClass.method3();
        //Knowledge point 1: static methods defined in the interface can only be called through the interface.
        CompareA.method1();
        //Knowledge point 3: if a method with the same name and parameter is declared in the subclass or implementation class, the inherited parent class and the implemented interface,
        // If the subclass does not override this method, it will call the method with the same name and parameter in the parent class by default
        //Knowledge point 4: if multiple interfaces are implemented in the implementation class, the interface defines methods with the same name and parameters. If the method is not rewritten, an error is reported - Interface conflict
        //This makes it necessary to override the methods in the interface
        subClass.method3();
        //Knowledge point 2: by implementing the object of the class, you can call the default method in the interface.
        // If the implementation class overrides the default method in the interface, the overridden method will still be called-- Class priority principle
        subClass.menthod2();
    }
}

class SubClass extends SuperClass implements CompareA,CompareB {

    public void method2() {
        System.out.println("SuperClass:Shanghai");
    }

    public void method3() {
        System.out.println("SuperClass:Shenzhen");
    }

    //Knowledge point 5: how to call the parent class in the subclass or implementation class method and the method to be rewritten in the interface.
    public void myMethod(){
        method3();//Call your own overridden method
        super.method3();//Calling a method in a parent class
        CompareA.super.method3();//Call the default method in the interface
    }
}

Topics: Java Design Pattern interface