Comparing abstract classes and interfaces

Source: Internet
Author: User

Abstract class:

(1) The abstract keyword can only be decorated with classes and methods and cannot be decorated with fields.

(2) Abstract class can not be instantiated (popular point, cannot be new), can only be inherited.

(3) Abstract classes can include properties, methods, constructor methods, initialization blocks, inner classes, enumeration classes, and ordinary classes, the normal method must be implemented, the variable can be initialized or uninitialized, but cannot be initialized and then re-assigned in the abstract class or manipulate the variable ( The variable can only be changed in a subclass )

(4) Abstract methods cannot be implemented in abstract classes.

(5) A class containing an abstract method must be drawn as an abstract class.

Interface:

(1) The member constants and methods of the interface must be public, ensuring that they are externally accessible.

(2) The method in the interface must be an abstract method.

(3) The interface does not involve details related to any specific instance, so the interface has no constructor method, cannot be instantiated, there are no instance variables, only static variables.

(4) The variables in the interface are common to all implementation classes, so they cannot be changed, only immutable (final) types, which are constants.

(5) The interface method decoration is public abstract by default, and the interface variable default adornment is public static final constant, and must be initialized.

Note: Final and abstract cannot appear at the same time

The difference between an abstract class and an interface:

(1) The interface is public, it cannot have private methods or variables, and abstract classes can have private methods or private variables.

(2) Different design concepts, abstract class means "is-a" (is a) relationship, interface means "has-a" (there is a) relationship.

(3) Implementing an interface must implement all the methods of the interface definition, while inheriting an abstract class can selectively rewrite the method to be used.

(4) An interface cannot have an implementation method, and an abstract class can have an implementation method.

(5) Variables in an interface (that is, constants) cannot be redefined, the variables in the abstract class are friendly by default (package is visible), and their values can be redefined in subclasses.

(6) Multiple interfaces may be implemented, but only one abstract class can be inherited.

Comparing abstract classes and interfaces

Related Article

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.