Differences between java abstract class interfaces

Source: Internet
Author: User

The class containing the abstract modifier is an abstract class, and the abstract class cannot create instance objects. Classes that contain abstract methods must be defined as abstract class. Methods in abstract class do not need to be abstract. Abstract methods defined in the abstract class must be implemented in the Concrete subclass. Therefore, there cannot be abstract constructor or abstract static methods. If the subclass does not implement all abstract methods in the abstract parent class, the subclass must also be defined as abstract.
An interface can be said to be a special case of an abstract class. All methods in an interface must be abstract. The method definition in the interface defaults to public abstract, and the member variable type in the interface defaults to public static final.
The following describes the syntax differences between the two.:
1. abstract classes can have constructor methods, and interfaces cannot have constructor methods.
2. abstract classes can contain common member variables. The interface does not contain common member variables.
3. abstract classes can contain non-Abstract Common methods. All methods in the interface must be abstract and cannot have non-Abstract Common methods.
4. The access type of abstract methods in abstract classes can be public, protected, and (default type, although
There is no error in eclipse, but it should not), but the abstract method in the interface can only be of the public type, and the default is the public abstract type.
5. abstract classes can contain static methods. Interfaces cannot contain static methods.
6. the abstract class and interface can contain static member variables. The access type of static member variables in the abstract class can be any, but the variables defined in the interface can only be of the public static final type, the default value is public static final.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
Next we will talk about the differences between the two in terms of application.:
Interfaces are mainly used to define communication contracts between modules in the system architecture design method. Abstract classes play a role in code implementation and can be reused. For example, the template method design pattern is a typical application of abstract classes, assume that all Servlet classes of a project must use the same method to determine permissions, record access logs, and handle exceptions, then an abstract base class can be defined, let all servlets inherit this abstract base class and complete permission judgment, access logging, and abnormal code in the service method of the abstract base class, in each subclass, only the business logic code is completed. The pseudocode is as follows: Copy codeThe Code is as follows: public abstract class BaseServlet extends HttpServlet
{
Public final void service (HttpServletRequest request, HttpServletResponse response) throws IOExcetion, ServletException
{
// Record access logs
// Determine Permissions
// If (with permission)
{
Try
{
DoService (request, response );
}
Catch (Excetpion e)
{
Record exception information
}
}
}
Protected abstract void doService (HttpServletRequest request, HttpServletResponse response) throws IOExcetion, ServletException;
// Note that the access permission is defined as protected, which is both professional and rigorous, because it is specially used for subclasses.
}
Public class MyServlet1 extends BaseServlet
{
Protected void doService (HttpServletRequest request, HttpServletResponse response) throws IOExcetion, ServletException
{
// This Servlet only processes the specific business logic code
}
}

Some code in the middle of the parent class method is uncertain and left to the subclass. The template method design mode is used.
Remarks: The idea of this question is to first explain the basic concepts of abstract classes and interfaces, then compare the syntax details of the two, and finally explain the differences between the two. The differences between the two syntaxes are as follows: the constructor, common member variables, and methods (including abstract methods) in a class, static variables, and methods, in terms of inheritance, the answers are compared one by one. Then, the answers are answered from the perspective of third party inheritance. In particular, a typical example is used to show your profound technical skills.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.