java interface (java interface)

Article directory

Interface Features

​Note that it is only for JDK8 before

  1. Methods in interfaces can have parameter lists and return types, and all methods have no method body.

  2. Interfaces have no constructors. Because constructors are used to create objects.

  3. All member variables in the interface are modified by public static final by default.

  4. Fields in an interface are only stored in the static storage area of ​​the interface, not belonging to the interface.

  5. The method in the interface can be declared as public or not declared, but the result will be processed according to the public type.

  6. All methods in the interface are modified by public abstract by default. You can also use protected, but not private.

  7. When implementing an interface, the defined method needs to be declared as public, otherwise it is the default access type, which is not allowed by the Java compiler.

  8. If all the methods in the interface are not implemented, then an interface is still created.

  9. Extending an interface to generate a new interface should use the keyword extends, and implementing an interface using implements.

  10. An interface inherits multiple interfaces at the same time (interface C extends A, B {} is possible.)

  11. The method in the interface is an abstract method (abstract), not a static method (static). All methods of the interface are abstract, and the abstract method is not static, and the method with static cannot be overridden, so the interface is defined in this way significance.

  12. A class that implements an interface must provide concrete implementations of all the methods in the interface.

Java (JDK) 8 interface

In the JDK8 version, the following three methods are allowed in the interface:

  • abstract modified abstract method: public abstract return type method name (parameter);
  • The default method modified by default: public default return type method name (parameter) {method body};
  • static modified static method: public static return type method name (parameter) {method body};

Java (JDK) 8 interface new features

  1. Non-abstract methods that can be modified with the default keyword in an interface . That is: the default method (or extension method), the implementation class can be overridden or not,
  2. The static keyword can be used in the interface to modify the static method of the interface. The implementation class and sub-interface cannot override the static method of the interface. The static method defined in the interface can only be called through the interface.

Java (JDK) 8 interface rules

public interface CompareA {
    
    
    //静态方法
    public static void method1(){
    
    
        System.out.println("CompareA:北京");
    }

    //默认方法
    public default void method2(){
    
    
        System.out.println("CompareA:上海");
    }
    
    public default void method3(){
    
    
        System.out.println("CompareA");
    }
}
  1. The static method defined in the interface can only be called through the interface
    public class SubClassTest {
          
          
        public static void main(String[] args) {
          
          
            SubClass s = new SubClass();
            //s.method1();  错误写法
            CompareA.method1();
        }
    }
    
    class SubClass implements CompareA{
          
          
    	
    }
    
  2. By implementing the object of the class, the default methods in the interface can be called.
  3. If the implementing class overrides the default method in the interface, the overridden method is still called when calling.

Method overriding of subinterface and parent interface

Interface static methods cannot be implemented by implementing classes and subinterfaces

​ Interface static methods cannot be overridden by implementing classes and sub-interfaces

The default (default) method can be implemented by implementing classes and subinterfaces

​ The sub-interface inherits the parent interface and overrides the default method of the parent interface.

The code example is as follows:

interface interfaceA{
    
    
     default void defaultMethod(){
    
    
        System.out.println("interfaceA defaultMethod 方法");
    }
}

interface interfaceB extends interfaceA{
    
    
    default void defaultMethod(){
    
    
        //通过接口名.super.方法名来调用父类
        interfaceA.super.defaultMethod();
        System.out.println("interfaceB defaultMethod 方法");
    }
}
interface interfaceC extends interfaceB{
    
    
    default void defaultMethod(){
    
    
        interfaceA.super.defaultMethod();
        interfaceB.super.defaultMethod();
        System.out.println("interfaceB defaultMethod 方法");
    }
}

If each overridden method called its super method, you would have a chain of calls. In general, an implementation can always override the default method without calling it super.

Variables and methods with the same name in the interface

​Note that the current directory is for JDK8 before.

1. A variable with the same name appears between the parent class and the interface or interface

Note that variables with the same name in the interface are constants (modified by public static final) and can only be 接口名.常量名called.

Problem 1: When implementing the interface and inheriting the same variable from the parent class , the subclass cannot make a choice.
Solution:

​ In the method of the subclass, it is displayed to indicate whether the variable to be output is a variable of the parent class or an interface. ( super/接口名.变量名).

The code example is as follows:
interface interfaceA{
    
    
    int value = 0;
}
class ClassA{
    
    
    int value=10;
}
interface interface2{
    
    
    int value = 100;
}
class Test extends ClassA implements interfaceA,interface2{
    
    
    public int getValue(){
    
    
        //return super.value;	//父类中的i
        //return A.value;		//接口A中的i
        return A2.value;		//接口A2中的i
    }
}
public class TestMain {
    
    
    public static void main(String[] args) {
    
    
        Test t = new Test();
        System.out.println(t.printI());
    }
}

2. There is a method with the same name in the parent class and interface

The code example is as follows:
interface interfaceA{
    
    
    public void methodA();
}
class ClassA{
    
    
     public void methodA(){
    
    
         System.out.println("ClassA中的methodA()方法");
     };
}

class Test extends ClassA implements interfaceA{
    
    
   	//注意:这里子类不重写接口的方法,也不会报错
    //子类直接继承父类和实现接口不实现接口方法不报错,说明这种情况下默认父类实现该方法
}
public class TestMain {
    
    
    public static void main(String[] args) {
    
    
        Test t = new Test();
       	t.methodA();
    }
}
//打印日志
//我是Test类中的methodA()方法
  • If the subclass does not override the method, the method in the parent class will be called first by default, and no error will be reported.

    Note: Here the parent class already has the method in the interface, and the method of the interface is not rewritten, and no error will be reported
    . method

  • If the subclass overrides this method, it is equivalent to overriding the methods in the parent class and the interface at the same time. Note: The method overridden by the subclass is called.

class Test extends ClassA implements interfaceA{
    
    
   	 @Override
    public void methodA() {
    
    
        System.out.println("Test中的methodA()方法");
    }
    
}
public static void main(String[] args) {
    
    
        Test t = new Test();
       	t.methodA();
    }
//打印日志
//Test中的methodA()方法
  • If the subclass wants to use the method of the parent class, it can passsuper.方法名
class Test extends ClassA implements interfaceA{
    
    
   	 @Override
    public void methodA() {
    
    
        super.methodA()
        System.out.println("Test中的methodA()方法");
    }
    
}
public static void main(String[] args) {
    
    
        Test t = new Test();
       	t.methodA();
    }
//打印日志
//ClassA中的methodA()方法
//Test中的methodA()方法

3. There is a method with the same name between the interface and the interface

1. The parameter list + return value are the same: the implementation class only needs to implement this method once
interface interfaceA{
    
    
    public void methodA();
}
interface interface2{
    
    
    public void methodA();

}
class Test implements interfaceA,interface2{
    
    
   	 @Override
     public void methodA(){
    
    
         System.out.println("Test中的methodA()方法");
     };
}
public class TestMain {
    
    
    public static void main(String[] args) {
    
    
        Test t = new Test();
        t.methodA();
    }
}
//打印日志
//Test中的methodA()方法
2. The parameter list is the same + the return value is different: the implementation class cannot directly implement the two methods (IDE reports an error).
interface interfaceA{
    
    
    public void methodA();
}
interface interface2{
    
    
    public int methodA();

}
class Test implements interfaceA,interface2{
    
    
    //编译期就会直接报错
   	//英文:'methodA()' in 'Test' clashes with 'methodA()' in 'InterfaceB';
    //attempting to use incompatible return type
   	//中文:'Test' 中的 'methodA()' 与 'InterfaceB' 中的 'methodA()' 冲突;
    //尝试使用不兼容的返回类型
    //无论重写哪一个接口中的方法都会报错,返回参数不同,定位不到哪个方法。
     @Override
     public void methodA(){
    
    
     };
}
3. The parameter list is different: the implementation class can implement two methods respectively
interface interfaceA{
    
    
    public void methodA();
}
interface interfaceB{
    
    
    public int methodA(String name);

}
class Test implements interfaceA,interfaceB{
    
    
 	//参数列表不相同,分别重写,构成Test类重载。
     @Override
     public void methodA(){
    
    
         System.out.println("Test中的methodA()方法");
     }
     @Override
     public void methodA(String name){
    
    
         System.out.println("Test中的methodA(value="+name+")方法");
     };
}
public class TestMain {
    
    
    public static void main(String[] args) {
    
    
        Test t = new Test();
		t.methodA();
       	t.methodA("fry");

    }
}
//打印日志
//Test中的methodA()方法
//Test中的methodA(value=fry)方法

Guess you like

Origin blog.csdn.net/fry3309/article/details/124440331