What is the difference between abstract class and interface?

Source: Internet
Author: User

(1) 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.

(2) An interface can be said to be a special case of an abstract class. All methods in the 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.

(3) 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 classes can access public, protected, and (default type, although there is no error in eclipse, but it should not), but the abstract methods in the interface can only be public, 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.

(4) Let's talk about the differences between the two in terms of application:

InterfaceMore importantly, it plays a role in the system architecture design method and is mainly used to define the communication contract between modules. WhileAbstract classIt plays a role in code implementation and can realize code reuse. 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 to allow all servlets
All inherit this abstract base class, and complete permission judgment, access logging, and exception handling code in the service method of the abstract base class. In each subclass, only the corresponding service 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 {records access logs for permission judgment 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 subclass}
Public class myservlet1 extends baseservlet {protected void doservice (httpservletrequest request, httpservletresponse response) throwsioexcetion, servletexception {specific business logic code processed by this servlet only }}

Some code in the middle of the parent class method is uncertain and left to the subclass. The template method design mode is used.
Note: The idea of this question is to explain the basic concepts of abstract classes and interfaces in general, and then compare the syntax details of the two,
Finally, let's talk about the differences between the two. The difference between the two syntax details is: first from the constructor of a class
Member variables and methods (including abstract methods), static variables and methods, and inheritance are compared and answered one by one.
The answer from the perspective of third-party inheritance, especially 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.