Review the past and learn the new: What is the difference between interfaces and abstract classes?

The following content of this article is based on JDK 8 version.

1. Interface introduction

An interface is an abstract type in the Java language that defines the public behavior of an object. Its creation keyword is interface. Methods and constants can be defined in the implementation of the interface. Its ordinary methods cannot be implemented with specific code. After JDK 8, static and default methods can be created in the interface, and these two Each method can have a default method implementation.

Interface definition:

package basic.person;

public interface IPerson {

    String info = "info";

    default void selectOne() {
        System.out.println("interface IPerson selectOne!");
    }

    void selectTwo();

    static void selectThree() {
        System.out.println("interface IPerson selectThree!");
    }
}

Interface implementation:

package basic.person;

public class PersonImpl implements IPerson {

    @Override
    public void selectTwo() {
        System.out.println("interface IPerson selectTwo!");
        System.out.println("interface IPerson info===" + info);
        selectOne();
        IPerson.selectThree();
    }
}

Test class:

package basic.person;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MainTest {

    @Before
    public void before(){
        System.out.println("junt test before init.");
    }

    //
    @Test
    public void test() {
        IPerson person =  new PersonImpl();
        person.selectTwo();
    }

    @After
    public void after(){
        System.out.println("junt test after init.");
    }
}

operation result:

"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe"************************ -junit4 basic.person.MainTest,test
junt test before init.
interface IPerson selectTwo!
interface IPerson info===info
interface IPerson selectOne!
interface IPerson selectThree!
junt test after init.

Process finished with exit code 0

in conclusion:

  1. In JDK 8, interfaces can define static and default methods, and these two methods can contain specific code implementations.
  2. To implement an interface, use the implements keyword.
  3. Interfaces cannot be instantiated directly.
  4. Variables defined in the interface default to public static final type.
  5. When the implementation class does not override the static and default methods in the interface, the method implementation of the interface is called by default.

2. Introduction to abstract classes

Abstract classes are similar to interfaces. They are also used to define the public behavior of objects, and they cannot be instantiated directly. The implementation keyword of abstract classes is abstract class, and subclasses use the extends keyword to inherit the parent class. 

Abstract class:

package basic.abstractclass;

public abstract class ParentPerson {

    int age =20;

    public abstract void  methodA();

    public void  methodB(){
        System.out.println("ParentPerson methodB init!");
    };

}

Implementation class

package basic.abstractclass;

public class Person extends ParentPerson {

    @Override
    public void methodA() {
        System.out.println("Person methodA init;");
        methodB();
    }
    
}

 Test class

package basic.abstractclass;

import basic.person.IPerson;
import basic.person.PersonImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MainTest {

    @Before
    public void before(){
        System.out.println("junt test before init.");
    }

    //
    @Test
    public void test() {
//        ParentPerson parentPerson = new ParentPerson();
        ParentPerson parentPerson = new Person();
        parentPerson.methodA();

    }

    @After
    public void after(){
        System.out.println("junt test after init.");
    }
}

Test Results:

"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" ........................
junt test before init.
Person methodA init;
ParentPerson methodB init!
junt test after init.

Process finished with exit code 0

in conclusion

  1. Abstract classes are declared using the abstract keyword.
  2. Abstract classes can contain ordinary methods and abstract methods. Abstract methods cannot be implemented by specific code.
  3. Abstract classes need to use the extends keyword to implement inheritance.
  4. Abstract classes cannot be instantiated directly.
  5. There are no restrictions on attribute control characters in abstract classes, and private type attributes can be defined.

3. Introduction to the difference between interfaces and abstract classes

3.1. Different definitions of keywords

Interfaces are defined using the keyword interface. Abstract classes are defined using the keyword abstract.

3.2. Different keywords for inheritance or implementation

The interface uses the implements keyword to define its specific implementation. Abstract classes use the extends keyword to implement inheritance.

3.3. The number of implementation (sub)class extensions is different

The implementation class of an interface can implement many interfaces, and the subclass of an abstract class can only inherit one abstract class. In the Java language, a class can only inherit from one parent class (single inheritance), but can implement multiple interfaces.

3.4. Different attribute access control characters

The access control character of the properties in the interface can only be public, and the properties in the interface are modified by public static final by default.

 There are no restrictions on attribute access control characters in abstract classes, and they can be any control characters.

3.5. Method control symbols are different

The default control symbol for methods in an interface is public and cannot be defined as other control symbols.

There are no restrictions on method controllers in abstract classes, and abstract methods cannot be modified with private.

3.6. Different implementation methods

Ordinary methods in interfaces cannot be implemented by specific methods. After JDK 8, static and default methods must be implemented by methods.

It can be seen from the above results that static or default methods will report an error if they are not implemented by a method, while ordinary methods will report an error if they are implemented by a method.

Ordinary methods in abstract classes can be implemented by methods, but abstract methods cannot be implemented by methods.

It can be seen from the above results that an ordinary method in an abstract class will report an error if there is no method implementation, and an abstract method will report an error if there is a method implementation. 

3.7. Use different static code blocks

Static code blocks cannot be used in interfaces .

Static code blocks can be used in abstract classes .

4. Summary

  1. The defined keywords are different.
  2. Subclasses inherit or implement keywords differently.
  3. Type extensions are different: abstract classes have single inheritance, while interfaces have multiple inheritance.
  4. Method access control characters: There are no restrictions on abstract classes, but abstract methods in abstract classes cannot be modified by private; interfaces have restrictions, and the default interface control character is public.
  5. Attribute method control symbols: There are no restrictions on abstract classes, but there are restrictions on interfaces. The default control symbol for interfaces is public.
  6. Method implementations are different: ordinary methods in abstract classes must be implemented, and abstract methods must not be implemented; while ordinary methods in interfaces cannot be implemented, but static and defualt methods in JDK 8 must be implemented.
  7. The use of static code blocks is different: abstract classes can have static code blocks, but interfaces cannot.

Guess you like

Origin blog.csdn.net/juanxiaseng0838/article/details/132421322