Do subclasses have to override methods defined in the interface?

Java abandons multiple inheritance in C++ and only supports single inheritance, but the effect of "multiple inheritance" can be achieved through interfaces. If a non-abstract subclass inherits an abstract class and implements an interface at the same time, and it happens that the non-abstract method contained in this abstract class is the same as the method in the interface, what will happen when the subclass uses this method? Let’s take a look at the first example:

Define an abstract classAbstractTest, containing a non-abstract methodtestThe return value type is void and the parameter type is String, as follows:

public abstract class AbstractTest {
    
    

    public void test(String testStr){
    
    
        System.out.println(testStr + ":Abstract 非抽象方法被调用");
    }

}

Define an interfaceInterfaceTest, defined in the interfacetest The return value type is void, and the method parameter type is String, which is the same as the method in the abstract class above, as follows:

public interface InterfaceTest {
    
    

    void test(String testStr);

}

Define a subclassTestClass, inherit abstract classAbstractTestImplement the interfaceInterfaceTest without rewriting the test method. Let’s take a look first, as follows:

public class TestClass extends AbstractTest implements InterfaceTest {
    
    

    public static void main(String[] args) {
    
    
        TestClass testClass = new TestClass();
        testClass.test("我调用了谁?");
    }
}

Execute the main method, and the program running results are as follows:
Insert image description here

Judging from the program running results, ourTestClass can not declare methods in the interface and can call the test method, and the specific implementation is abstract test method in classAbstractTest, Conclusion: When the non-abstract method in the abstract class is the same as the method in the interface, the subclass that inherits the abstract class and implements the interface does not need to declare the method in the interface, and the method in the abstract is specifically called. This reflects the "Class Priority" rule in Java.

So what happens when a subclass overrides the test method?

Bottom GeneralTestClassKusaku modification, duplicatetestMethod, as below:

public class TestClass extends AbstractTest implements InterfaceTest {
    
    

    @Override
    public void test(String str){
    
    
        System.out.println(str +"子类重写的test方法被调用");
    }

    public static void main(String[] args) {
    
    
        TestClass testClass = new TestClass();
        testClass.test("我调用了谁?");
    }
}

Execute the main method again, and the program operation results are as follows:

Insert image description here

Conclusion: When a subclass overrides the test method, whether it is a non-abstract method in an abstract class or a method in an interface, the subclass When a class is rewritten, the specific call is the rewritten method of the subclass. This actually ensures the backward compatibility and high scalability of Java and embodies the polymorphism in Java. Concept.

Let’s modify our example and add an abstract methodtestMethod in the abstract classAbstractTest and add the same method in the interface, as follows:

Abstract classAbstractTestis as follows:

public abstract class AbstractTest {

    public void test(String testStr){
        System.out.println(testStr + ":Abstract 非抽象方法被调用");
    }

    public abstract void testMethod(String str);

}

InterfaceInterfaceTestas below:

public interface InterfaceTest {

    void test(String testStr);

    void testMethod(String str);

}

Since abstract methods in abstract classes and methods in interfaces must be rewritten, subclasses need to rewrite thetestMethod method, as follows :

public class TestClass extends AbstractTest implements InterfaceTest {
    
    

    @Override
    public void test(String str){
    
    
        System.out.println(str +"子类重写的test方法被调用");
    }

    @Override
    public void testMethod(String str) {
    
    
        System.out.println(str + "抽象类的抽象方法和接口方法必须重写");
    }

    public static void main(String[] args) {
    
    
        TestClass testClass = new TestClass();
        testClass.test("我调用了谁?");
        testClass.testMethod("Hello?");
    }
}

Execute the main method, and the program running results are as follows:

Insert image description here

Conclusion: When the abstract method in the abstract class is the same as the method in the interface, the subclass must override the method to reflect the abstract class in Java< Characteristics of a i=2>&interface.

Based on the above example, we can see that interface is a more abstract form than abstract class. When the non-abstract method in the abstract class is the same as that in the interface, the subclass does not need to explicitly implement the method, but the specific The call must override the method, but in this special case, the subclass inherits the method in the abstract class for implementation.
Abstract class and Interface

Based on the above example, we can see that interface is a more abstract form than abstract class. When the non-abstract method in the abstract class is the same as that in the interface, the subclass does not need to explicitly implement the method, but the specific call must be rewritten. This method is provided, but in this special case, the subclass inherits the method in the abstract class for implementation.

おすすめ

転載: blog.csdn.net/chen15369337607/article/details/126377329