What is the difference between Java abstract classes and interfaces

Source: Internet
Author: User
Tags modifiers

What is the difference between an abstract class and an interface?

1, abstract class in the Java language is represented by an inheritance relationship, a subclass can only inherit a parent class. However, multiple interfaces can be implemented.

2, in the abstract class can have their own member variables and non-abstract class methods, but the interface can only exist static immutable (public static final) member data (only generally not defined in the interface member data). And all of its methods are abstract.

3, the subclass of the abstract class can partially implement the abstract method in the parent class. But they are abstract classes themselves; When a class implements a special interface, it defines a method for all such interfaces.

4, the abstract class and the interface reflects the design concept is different, the abstract class represents is "is-a" the relation, but the interface represents is "the like-a" the relation.

5、abstract methods can have Public,protectedand thedefaultthese modifiers, the interface method default modifier, are public . Other modifiers cannot be used.

6. Add a new method:Assume that new methods are added to the abstract class. The ability to give it a default implementation without changing the code today.

If you add a method to an interface, you must change the class that implements the interface


When do I use an abstract class for an interface?

1. Suppose you are estimating that you want to create multiple version numbers of a component, create an abstract class. Abstract classes provide a simple way to control the component version number. By updating the base class, all inheriting classes are actively updated with the changes themselves. There is one more aspect. The interface cannot be changed once it is created. Assuming a new version number of the interface is required, a completely new interface must be created.

2. Assume that the functionality created will be used across a wide range of heterogeneous objects, using an interface.

Abstract classes should be used primarily for closely related objects. Interfaces are best suited to provide common functionality for unrelated classes.

3. Suppose you want to design a small and concise function block. The interface is used.

Suppose you want to design a large functional unit, use an abstract class.

4. If you want to provide common implemented functionality across all implementations of a component, use an abstract class. The abstract class agrees to partially implement the class. The interface does not include implementations of whatever members.


What is the difference between Java abstract classes and interfaces

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.