javaSE notes - Interface

Posted by drimades on Sun, 12 Apr 2020 20:21:16 +0200

An interface cannot create an object, but it can be implemented (` implements', similar to being inherited). A class that implements an interface (which can be regarded as a subclass of an interface) needs to implement all the abstract methods in the interface, create the class object, and then call the method, otherwise it must be an abstract class.

 

Interface: is a collection of functions (methods are defined in the interface)

The. java file is also used to define the interface; the. class file is also generated by compilation

Defining interfaces using the keyword interface

Modifier interface interface name{

Abstract methods;

Default method; (added after JDK8)

Static method; (new after JDK8)

}

 

Use of interface:

1. The interface cannot be used to create objects

2. You can define an implementation class, implement (inherit) the interface, rewrite the abstract methods in the interface, and create an implementation class object to use

 

Abstract method: no method body, method modified by abstract

The default modifier public abstract can be omitted

public abstract return value type method name (parameter);

 

Note:
The abstract method modifier in the interface can be omitted or not written, which is also public abstract by default

It is suggested to write out and enhance reading ability

public interface MyInter {
    //Define abstract methods
    public abstract void show01();

    public abstract int show02();

    void show03(String s);
}

 

Define the implementation class of the interface

Use format: use class to implement interface and rewrite abstract methods in interface

public class implements the class name implements interface{

Rewrite abstract methods in interface;

      }


Note:

Implementation is similar to inheritance. If a class implements an interface, it can inherit all non private members in the interface

public class MyInterImpl implements MyInter{
    @Override
    public void show01() {
        System.out.println("In the implementation class rewriting interface show01 Abstract method!");
    }

    @Override
    public int show02() {
        System.out.println("In the implementation class rewriting interface show02 Abstract method!");
        return 0;
    }

    @Override
    public void show03(String s) {
        System.out.println("In the implementation class rewriting interface show03 Abstract method!"+s);
    }
}

 

 

public class Demo01MyInter {
    public static void main(String[] args) {
        //Interface cannot create object to use 'MyInter' is abstract; cannot be instantiated
        //MyInter mi = new MyInter();

        //Create the implementation class object of the interface
        MyInterImpl my = new MyInterImpl();
        my.show01();
        my.show02();
        my.show03("Zhao Si");
    }
}

 

 

Default method

Definition format:

Modifier default return value type method name (parameter){

Method body;

      }

 

Note:

The modifier default of the default method cannot be omitted

public interface MyInter {
    public default void show01(){
        System.out.println("MyInter Default in interface show01 Method");
    }

    public default  void show02(){
        System.out.println("MyInter Default in interface show02 Method");
    }
}

 

 

    1. Use of interfaces with default methods: define implementation classes, implement interfaces, selectively override default methods, and create implementation class objects
    2. Overridden the default method: using the overridden method of the implementation class
    3. Default method not overridden: use default method inherited from interface

Note:

The implementation class overrides the default method in the interface, removing the default modifier

 

public class MyInterImpl implements MyInter{
    @Override
    public void show01() {
        System.out.println("Implementation class overridden MyInter Interface show01 Default method");
    }
}

 

 

public class Demo01MyInter {
    public static void main(String[] args) {
       //Create the implementation class object of the interface
        MyInterImpl my = new MyInterImpl();
        my.show01();//Implement class overridden
        my.show02();//Inherited from interface
    }
}

 

 

Static method

Definition format:

Modifier static return value type method name (parameter){

Method body

      }

Note:

        • Static keyword cannot be omitted when defining static method
        • Define interface usage with static methods:
        • Static members belong to classes (interfaces), so they can be directly used by interface name and method name (parameters)

 

public interface MyInter {
    public static void show01(){
        System.out.println("MyInter Static of interface show01 Method");
    }

    public static String show02(int a){
        System.out.println("MyInter Static of interface show01 Method"+a);
        return "Ha-ha";
    }
}

 

 

public class MyInterImpl implements MyInter{
    //@Override//Method does not override method from its superclass
    public static void show01(){
        System.out.println("MyInterImpl Class static show01 Method");
    }
}

 

 

Static methods can only be called by the interface name, not by the class name of the implementation class or the object call of the implementation class

Static methods cannot be overridden, belong to the class| interface itself, and cannot be inherited and used by implementation classes. The static method defined in the implementation class belongs to the implementation class itself

public class Demo01MyInter {
    public static void main(String[] args) {
        //Through interface name.Method name(parameter)Calling static methods in an interface
        MyInter.show01();
        String s = MyInter.show02(10);
        System.out.println(s);

        MyInterImpl.show01();
    }
}

 

 

Multi implementation of interface

Syntax

Class class name [extensions parent class name] implements interface name 1, interface name 2, interface name 3{

/ / override the abstract method in the interface [required]

/ / override the default method in the interface [optional if no duplicate name]

    }

 

Abstract method

When there are multiple abstract methods in an interface, the implementation class must override all abstract methods. If the abstract method in multiple interfaces has the same name, it only needs to be overridden once.

 

Default method

In the interface, when there are multiple default methods, the implementation class can inherit and use them. If the default method has a duplicate name, it must be overridden once.

 

Static method

In an interface, static methods with the same name do not conflict because they can only be accessed through their respective interface names.

 

Priority issues

When a class inherits a parent class and implements several interfaces, the member method in the parent class and the default method in the interface have the same name, and the child class selects the member method that executes the parent class nearby.

 

Multiple inheritance of interfaces

  • The 'extends' keyword is used for interface inheritance, and the child interface inherits the method of the parent interface. If the default method in multiple parent interfaces has the same name, the child interface needs to be overridden once
  • When a sub interface overrides the default method, the default keyword can be retained.
  • When a subclass overrides the default method, the default keyword cannot be preserved.

 

Characteristics of other members

- in the interface, member variables cannot be defined, but constants can be defined. Their values cannot be changed. public static final is used by default.

- in the interface, there is no constructor, so the object cannot be created.

- there are no static code blocks in the interface.

 

Abstract classes and interfaces

    • A class can only inherit one direct parent class (maybe an abstract class), but it can implement multiple interfaces, which makes up for Java's single inheritance
    • Abstract class is the common content of inheritance system, and interface is the extended function of inheritance system

Topics: Java