Quick learning 1.8 front and rear interfaces

Posted by Sphynx on Sun, 19 Dec 2021 08:10:09 +0100

JDK1. Before 8

1. Interface declaration format

[Access modifier ]  interface Interface name   [extends  Parent interface 1, parent interface 2]  {
         Constant definition;       
         Method definition;
}

2. Code

/*
    1.Class is a class and interface is an interface. They are concepts at the same level
    2.There is no constructor in the interface
    3.How interfaces are declared: interface
    4.At jdk1 Before 8, the interface had only two parts
    (1)Constant: public static final
    (2)Abstract method: public abstract
    Note: modifiers can be omitted and not written. IDE will complete them by default, but beginners are advised to write them to increase the impression
 */
public interface TestInterface {
    //constant
    /*public static final*/ int NUM = 10;

    //Abstract method
    /*public abstract*/ void a();
    /*public abstract*/ void b(int num);
    /*public abstract*/ int c(String name);
}

interface TestInterface02{
    void e();
    void f();
}

/*
    5.What is the relationship between classes and interfaces? Implementation relationship: class implementation interface
    6.Once an interface is implemented, the implementation class overrides all abstract methods of the interface
    7.If you do not override all abstract methods, this class can become an abstract class
    8.java There is only single inheritance, but how many implementations does java have
    A class inherits from other classes, but can only inherit one parent class
    However, if a class implements an interface, multiple interfaces can be implemented
    9.Writing method: inherit first and then implement: extends person implements, testinterface, testinterface02
 */

class Student extends Person implements TestInterface,TestInterface02{
    @Override
    public void a() {
        System.out.println("------1");
    }

    @Override
    public void b(int num) {
        System.out.println("------2"+num);
    }

    @Override
    public int c(String name) {
        return 10;
    }

    @Override
    public void e() {
        System.out.println("------3");
    }

    @Override
    public void f() {
        System.out.println("------4");
    }
}


class Test{
    public static void main(String[] args) {
        //Interface cannot be instantiated, that is, object cannot be created
//        TestInterface t = new TestInterface();
        TestInterface02 t = new Student(); // Interface points to implementation class -- "polymorphism"

        //11. How to access constants in the interface
        System.out.println(TestInterface.NUM);
        System.out.println(Student.NUM);

        Student s = new Student();
        System.out.println(s.NUM);
        TestInterface t2 = new Student();
        System.out.println(t2.NUM);
    }
}

3. Function of interface

The interface is mainly used to define rules, and the implementation class is responsible for implementation;
The difference from an abstract class is that it is an interface rather than a class

4. Conceptual analysis

Inheritance: the inheritance of a child class from its parent class
Implementation: the implementation of the interface by the implementation class

For example:
Is "mobile phone" a "camera"
Inheritance: the mobile phone extends the relationship between the camera 'is-a', and the mobile phone is a camera

The above expression is not good enough
Implementation: the mobile phone implement s the relationship between the camera 'has-a', and the mobile phone has the function of camera

Case: plane, bird, kite
Define an interface: Flyable [they can all realize the function of flying, but obviously they can't be classified into one class. It's obviously wrong to say that they inherit from a flight class, which should be understood slowly in practice]

5. Polymorphic applications

(1) The parent class is used as the formal parameter of the method and passed into the object of the specific subclass
(2) As the return value of the method, the parent class returns the object of the specific subclass
(3) The interface is used as a formal parameter of the method and passed into the object of the concrete implementation class
(4) Interface is used as the return value of the method to return the object of the specific implementation class

6. Difference between interface and abstract class

The brief description of Baidu can be briefly understood, because most of them now use the version after 1.8, which will be different

JDK1. Add non abstract methods after 8

Review jdk1 Before 8, the interface only had two parts
1. Constant: fixed modifier: public static final
2. Abstract method: fixed modifier: public abstract

Non abstract method modified by public default

  1. The default modifier must be added, otherwise an error will be reported
  2. If the implementation class wants to rewrite the non abstract method in the interface, the default must not be added, otherwise an error will be reported
public interface TestInterface18 {//Interface after 1.8
    //constant
    public static final int NUM = 10;
    //Abstract method
    public abstract void a();
    //Non abstract method modified by public default
    public default void b(){
        System.out.println("----public default,method---");
    }
}

class  A implements TestInterface18 {

    public void c(){
        //Use the interface b method
        b();//sure
        //super.b();// No, the interface cannot be regarded as the parent class of this class
        TestInterface18.super.b();//Yes, special interface method calling methods
    }

    @Override
    public void a() {
        System.out.println("Interface rewritten a Abstract methods in");
    }

    @Override
    public void b() {
        
    }
}

Static method

  1. static cannot be omitted
  2. Static methods cannot be overridden
public interface TestInterface3 {
    //Constant:
    public static final int NUM = 10;
    //Abstract method:
    public abstract  void a();
    //public default non abstract method;
    public default void b(){
        System.out.println("-----TestInterface3---b");
    }
    //Static method
    public static void c(){
        System.out.println("-----TestInterface3 Static method");
    }
}

class Demo implements TestInterface3{

    @Override
    public void a() {
        System.out.println("Rewritten a method");
    }

    public static void c(){
        System.out.println("Demo Static methods in");
    }
}

class C{
    public static void main(String[] args) {
        Demo d = new Demo();
        d.c();
        Demo.c();
        TestInterface3.c();
    }
}
Operation results:
Demo Static methods in
Demo Static methods in
-----TestInterface3 Static method

Why add non abstract methods to the interface?

If only abstract methods can be added to the interface, changing the interface will have a great impact on the implementation class, and all implementation classes will be rewritten.
Now adding non abstract methods to the interface has little impact on the implementation class. If you want to call it, you can call it.

Topics: Java interface