Introduction to the differences between the Java abstract class interface

  • 2020-04-01 01:07:56
  • OfStack

A class with the abstract modifier is an abstract class that cannot create an instance object. A class containing abstract methods must be defined as abstract class, and methods in the abstract class class need not be abstract. Abstract methods defined in the abstract class must be implemented in Concrete subclasses, so you cannot have abstract constructors or abstract static methods. If the subclass does not implement all the abstract methods in the abstract parent class, then the subclass must also be defined as the abstract type.
An interface can be said to be a special case of an abstract class, and all methods in the interface must be abstract. The method definition in the interface defaults to public abstract type, and the member variable type in the interface defaults to public static final.
Let's compare the grammatical differences between the two :
Abstract classes can have constructors, and interfaces cannot have constructors.
2. There can be ordinary member variables in the abstract class, and no ordinary member variables in the interface
3. Abstract classes can contain non-abstract ordinary methods, all methods in the interface must be abstract, there can be no non-abstract ordinary methods.
The access types of abstract methods in an abstract class can be public, protected, and (default types, though)
Eclipse does not report errors, but should not), but the abstract methods in the interface can only be of public type, and by default are of public abstract type.
5. Static methods can be included in abstract classes, not in interfaces
6. Both abstract class and interface can contain static member variables. The access type of static member variables in the abstract class can be arbitrary, but the variables defined in the interface can only be of public static final type, which is the default public static final type.
7. A class can implement multiple interfaces, but can only inherit from one abstract class.
The following is the difference between the two applications :
Interfaces play more of a role in the system architecture design approach, mainly used to define the communication contract between modules. And abstract class play a role in code, can implement code reuse, for example, the template method design pattern is a typical application of the abstract class, assuming that all the Servlet class a project shall be conducted in the same way permissions, access log record and handle exceptions, you can define an abstract base class, let all the Servlet inherit the abstract base class, in the service methods of an abstract base class complete authority to judge, record access log and exception handling code, just finish their business logic in each subclass code, pseudo code is as follows:
 
public abstract class BaseServlet extends HttpServlet 
{ 
public final void service(HttpServletRequest request, HttpServletResponse response) throws IOExcetion,ServletException 
{ 
//Logging access log
//Make permission judgment
//If (with permissions)
{ 
try 
{ 
doService(request,response); 
} 
catch(Excetpion e) 
{ 
 Record abnormal information  
} 
} 
} 
protected abstract void doService(HttpServletRequest request, HttpServletResponse response) throws IOExcetion,ServletException; 
//Note that the access is defined as protected, which is both professional and rigorous, since it is reserved for subclasses
} 
public class MyServlet1 extends BaseServlet 
{ 
protected void doService(HttpServletRequest request, HttpServletResponse response) throws IOExcetion,ServletException 
{ 
//This Servlet deals only with the specific business logic code
} 
} 

A section of code in the middle of the parent method is not certain, and is left to the subclass to design the pattern using the template method.
note : the idea is to explain the basic concepts of abstract classes and interfaces in general, then to compare the syntax details of the two, and finally to explain the differences between the two applications. Comparative syntax details difference method is: start with a class constructors, ordinary member variables and methods (including an abstract method), static variables and methods, inheritance and so on six aspects one by one to answer, and then from the perspective of a third party to inherit the answer, especially in the end, a typical example to show his profound technical strength.

Related articles: