Difference between abstract class and interface?

A class with the abstract modifier is an abstract class, an instance object that cannot be created by an abstract class. A class with an abstract method must be defined as an abstract class, and the methods in an abstract class class do not have to be abstract. Abstract methods defined in abstract classes must be implemented in concrete subclasses, so there cannot be abstract constructors or abstract static methods. If the subclass does not implement all the abstract methods in the abstract superclass, then the subclass must also be defined as abstract.  
An interface can be said to be a special case of an abstract class, and all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to the public static final type.  
Let's compare the grammatical differences between the two:  
1. An abstract class can have a constructor, but an interface cannot have a constructor.  
2. An abstract class can have ordinary member variables, but there is no ordinary member variable in an interface.  
3. An abstract class can contain non-abstract ordinary methods. All methods in an interface must be abstract and cannot have non-abstract ordinary methods.  
4. The access type of the abstract method in the abstract class can be public, protected and (the default type, although  
no error is reported under eclipse, but it should not work), but the abstract method in the interface can only be of the public type, and the default is public abstract type.  
5. An abstract class can contain static methods, but an interface cannot contain static methods  
. 6. Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but the variables defined in interfaces are only Can be a public static final type, and the default is a public static final type.  
7. A class can implement multiple interfaces, but can only inherit from one abstract class.  
Next, let's talk about the difference between the two applications: the  
interface plays a role in the system architecture design method, and is mainly used to define the communication contract between modules. Abstract classes play a role in code implementation and can realize code reuse.  

For example, the template method design pattern is a typical application of abstract classes. Assuming that all servlet classes in a project must use the same method to determine permissions, record access logs, and handle exceptions, you can define an abstract base class to let All servlets inherit this abstract base class, and complete the code for judging permissions, recording access logs and handling exceptions in the service method of the abstract base class, and only completes their own business logic code in each subclass

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325098904&siteId=291194637