18th: interface is better than abstract class

Source: Internet
Author: User

Java provides two mechanisms that can be used to define types that allow multiple implementations: interfaces and abstract classes. The most obvious difference between these two mechanisms is that abstract classes allow implementations of certain methods to be included,

However, the interface is not allowed. A more important difference is that, in order to implement a type defined by an abstract class, a class must be a subclass of an abstract class, that is, using the Extends keyword inheritance.

Any class, as long as it defines all the necessary methods, and adheres to the general conventions, it is allowed to implement an interface, regardless of where the class is at the class level, the interface implementation uses

Keyword implements. Because Java only allows single inheritance, abstract classes are greatly constrained as type definitions.

Existing classes can easily be updated to implement the new interface.

An interface is an ideal choice for defining mixin (mixed types).

Interfaces allow us to construct a type framework that is not hierarchically structured.

The advantage of abstract classes is that we can provide implementations for some methods, and define methods that require client-side programmers to be defined as abstractions, and those that are specifically implemented in abstract classes

Method is based on a method defined as an abstraction, that is, a concrete implementation method, simply invoking an abstract method, or invoking an abstract method after simple logic. This simplifies the client

Programmers work under pressure, not all methods like interfaces need to be implemented by the client programmer.

The advantage of interfaces is that a class can implement multiple interfaces, rather than an abstract class, inheriting only one.

Combining the advantages of the two, the introduction of Skeleton implementation Class (Abstractinterface), skeleton implementation class an abstract class, this abstract class implementation needs to implement the interface, because the skeleton abstract class is an abstract class

Can contain abstract methods, then we define the interface needs to be implemented by the client programmer method as an abstract method, and the interface is only need to invoke other abstract methods, in the skeleton

The implementation class, defined as a concrete method, provides the implementation. By combining the two, skeleton implementation classes have the advantage of interfaces and abstract classes.

In the JDK's collection framework, there are a number of such skeleton implementation classes: Abstractcollection,abstractset,abstractlist,abstractmap

18th: interface is better than abstract class

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.