The difference between an abstract class and an interface

Source: Internet
Author: User

Interface(interface) can be said to be Abstract classA special case in which all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final. In addition, interfaces and abstract classes have difference: 1. Abstract classes can have construction methods, and interfaces cannot have constructors. 2. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods. 3. There can be ordinary member variables in an abstract class, and there is no ordinary member variable 4 in the interface. The access type of an abstract method in an abstract class can be public,protected and the default type of 5. A static method can be included in an abstract class, and the interface cannot contain static method 6. Both abstract classes and interfaces can contain static member variables, and the access type of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types, and public static final type 7 is the default. A class can implement multiple interfaces, but can inherit only one abstract class. There are also some differences in application: The interface is more in the system architecture design method to play a role, mainly used to define the communication contract between the modules. Abstract classes play a role in the implementation of code, can implement code reuse, for example, template method design pattern is a typical application of abstract class, it is assumed that all the servlet classes of a project to use the same way to determine permissions, log access logs and handle exceptions, you can define an abstract base class, Let all servlets inherit this abstract base class, and in the service method of the abstract base class, complete the permission judgments, log access logs, and code that handles exceptions, and only complete their own business logic code in each subclass.

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.