The difference between Java interfaces and abstract classes

Source: Internet
Author: User

Abstract class

Interface

Instantiation of

No

No

Class

An inheritance relationship in which a class can use only one inheritance relationship. Multiple inheritance can be implemented by inheriting several interfaces

A class can implement multiple interface

Data members

can have their own

Static cannot be modified that must be static final, which is generally not defined here

Method

Can be private, non-abstract methods, must be implemented

Not private, default is Public,abstract type

Variable

Can have private, default is friendly type, its value can be redefined in subclass, can also be re-assigned value

Not private, default is public static final type, and must give its initial value, the implementation class can not be redefined, cannot change its values.

Design concept

Represents a "is-a" relationship

Represents a "like-a" relationship

Realize

Need to inherit, to use extends

To use implements

Where to use the interface

A. Classes and classes require a specific interface to reconcile, rather than how they are implemented.

B. As an identity that is capable of implementing a specific function, it can also be a purely identifying interface method that does not exist.

C. A set of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.

D. A specific number of functions need to be implemented, and there may be no connection between these features at all.

What is the use of abstract classes?

In a word, it can be used in situations where both a uniform interface is required and an instance variable or a default method is required. The most common are:

A. Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use the abstract class to define a set of method bodies, even an empty method body, and then select the methods that you are interested in to overwrite by subclasses.

B. In some cases, a purely interface does not satisfy the coordination between classes and classes, and the variables that represent states in a class must differentiate between different relationships. Abstract mediation can be a good way to meet this point.

C. Standardize a set of mutually coordinated methods, some of which are common, state-independent, shareable, and do not need subclasses to implement separately, while others require individual subclasses to implement specific functions according to their own specific state.

The difference between Java interfaces and abstract classes

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.