The difference between an abstract class and an interface

Source: Internet
Author: User

On the difference between abstract class and interface, I was bothered by this problem for a long time when I was a beginner in Java. Search the Internet for a variety of answers, coupled with their own understanding, today to tidy up, if there is a mistake, also please high finger!

Generally speaking, abstract class is a special case of ordinary class, has the characteristics of ordinary class and no, and the interface represents a specification, standard, used for declaring methods, storing some static variables that cannot be changed.

The following is a summary of the syntax and application of the two two aspects:

1. Grammatical differences:

1) Construction Method: Abstract class can have, interface cannot have.

2) member variables: Abstract classes can have normal member variables, or static member variables, and there can be only variables that are modified by default (public static final).

3) Methods: Abstract classes can have ordinary methods, static methods, or can have abstract methods, the methods in the interface must be abstract methods.

4) Access rights: The access rights of static member variable in abstract class can be arbitrary, the access permission of abstract method is not private, other can be, the access permission of abstract method in interface is only public.

5) Inheritance and implementation: A class can inherit only one abstract class, but it is possible to implement multiple interfaces.

If you do not understand, you can write a separate abstract class and an interface, from the above several aspects to verify, deepen understanding.

2. Differences in application:

1) Abstract classes play a role in code implementation and can implement code reuse.

Template method Design pattern is a typical application of abstract class. Assuming that all of the servlet classes of a project have the same way of making permission judgments, logging access logs, and handling exceptions, you can define an abstract class so that all servlets inherit the abstract class and do the above 3 work in the service method of the abstract class. In each subclass, however, only the respective business logic code is completed.

A section of the parent method of the code is not certain, leaving the sub-class to do, it is necessary to use the template method design pattern.

2) The interface plays a role in system architecture design, which is mainly used to define the communication contract between modules.

PS: Listen to the piano, knocking on the beloved code and technical knowledge points, a kind of composing scientific works of art bright, 2333 ...

The difference between an abstract class and an interface

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.