Java Abstract class vs interface

Source: Internet
Author: User

Preface

Abstract class and interface are Java used to describe the abstraction of the body, do not know whether there are classmates and I have the same grammatical level of the difference between the two and how to choose the two there is still a doubt, anyway, let me introduce in detail the abstract Class and interface.

Understanding Abstract Classes

In object-oriented concepts, all objects are described by a class. But this is not the case, not all classes are used to describe objects. Because there may not be enough information in this class to describe a specific object, such a class is an abstract class (PS: Note that the abstract class here is not a single-finger abstract class). Abstract classes are often used to describe the abstract concepts we derive from analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same.

abstract class and interface syntax differences

The differences between abstract classes and interfaces are as follows:

    1. Abstract classes can have normal member variables, while interfaces generally do not have member variables. If you are not inserting member variables in an interface, the type of the member variable must be a static final type.
    2. Abstract classes can contain non-abstract ordinary methods, and all methods in an interface must be abstract.
    3. Abstract methods in an abstract class can have access types that are public, protected, or package access types. The abstract method in an interface can only be a public abstract type, and the method of the interface is preceded by a modifier, and the public abstract type is the default.
    4. A class can implement multiple interfaces (the compromise implementation of multiple inheritance), but only one abstract class is inherited.
the difference between the use of abstract class and interface

Abstract class design embodies the "is-a" of the relationship, and interface embodies the "has-a" relationship.

When do I use the interface?

You want to expand the things I give you, you have to implement the necessary interfaces. For example, an object that implements the comparable interface can be sorted directly using the Sort method Collections.sort (list list).

When do I use abstract classes?

If you have an abstract class, it provides a lot of common functionality and abstracts out the methods that each subclass needs to implement itself. And you are designing something that is based on this class, and that implementation allows you to inherit this abstract class and then implement your own unique approach.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Abstract class vs 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.