11) Java abstract class and interface, abstractinterface

Source: Internet
Author: User

11) Java abstract class and interface, abstractinterface

Abstract class and interface  The class that contains 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 are not necessarily abstract. Abstract class Defines abstract methods must be implemented in the Concrete subclass. Therefore, Abstract constructor methods or abstract static methods are not allowed.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 compares 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. abstract class abstract methods can access public, protected, and (default type, although there is no error in eclipse, but it does not), but the abstract method in the interface can only be public type, the default value is public abstract.
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:
Public abstract class BaseServlet extends HttpServlet {

Public final void service (HttpServletRequest request, HttpServletResponse response) throws IOExcetion, ServletException {

// Record access logs for permission judgment

If (with permissions ){

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 rigorous because it is specially used for subclass

}

 

Public class MyServlet1 extends BaseServlet {

Protected void doService (HttpServletRequest request, HttpServletResponse response) throws IOExcetion, ServletException {

This Servlet only processes 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.

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.