Abstract class and interface

Source: Internet
Author: User

Class that contains the abstract modifier is an abstract class, and the abstract class cannot create an instance object. A class containing an abstract method must be defined as an abstract class, and the method in the abstract class does not have to be abstract. Abstract class definitions must be implemented in a specific (concrete) subclass, so there can be no abstract constructor or abstract static method. If the subclass does not implement the abstract method in the abstract parent class, the subclass must also be defined as an abstract type.

An interface (interface) can be described as a special case in an abstract class, and 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.

The syntax differences between abstract class and interface:
1. Abstract classes can have a constructor method (non-abstract) and cannot have a constructor method in an interface.

2. There can be ordinary member variables in an abstract class, and there are no ordinary member variables in the interface (the member variables in interface default to: public static final).

3. Abstract classes can contain non-abstract ordinary methods, and all methods in an interface must be abstract (default: Public abstract).

4. The access type of an abstract method in an abstract class can be public, protected, but the variables defined in the interface are public only, and the default is public abstract.

5. Abstract classes can contain static methods, which cannot contain static methods (because all methods in the interface are public abstract, require class implements, implement an abstract method, and the static method cannot be overridden, so it cannot be static).

6. A class can implement multiple interfaces, but can inherit only one abstract class.

The difference between the abstract class and the interface application:

Interfaces are more useful in system architecture design, primarily for defining communication contracts between templates.

Abstract classes play a role in code implementation and can implement code reuse. For example, a template method design pattern is a typical application of an abstract class, assuming that all servlet classes of a project have the same way of determining permissions, logging access logs, and handling exceptions, then you can define an abstract base class that accomplishes permission judgments in the service methods of the abstract base class. The code that logs access logs and handles exceptions requires only the unique business logic code to be completed in each subclass.

Abstract class and 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.