Description of abstract class and interface in Java and Their Similarities and Differences

Source: Internet
Author: User
Tags define abstract

(1) Overview
Abstract class and interface support the definition of abstract classes in Java. It is precisely because of the existence of these two mechanisms

Only in this way can Java provide powerful object-oriented capabilities. Abstract class and interface support the definition of abstract classes

Therefore, many developers Define abstract classes and interfaces

The selection is relatively casual. In fact, there is a big difference between the two. Their choices even reflect the essence of the problem field.

Understanding of the design intent is correct and reasonable.


Abstract class and interface are used for abstract classes in Java language (the abstract classes in this article are not

It represents an abstract body, while abstract class is a method used to define abstract classes in Java.

Yes. So what is an abstract class? What are the benefits of using an abstract class?

In the concept of object-oriented, we know that all objects are depicted through classes, but this is not the case. Not all

Class is used to depict objects (class specific), if a class does not contain enough information to depict a specific object, such

Class is an abstract class. Abstract classes are often used to represent the abstract concepts we have come up with in the analysis and design of problem domains.

Different, but essentially identical abstract of specific concepts. For example, if we develop a graphic editing software, we will find the problem area.

There are some specific concepts such as circle and triangle, they are different, but they all belong to the concept of shape, the concept of shape in

The problem field does not exist. It is an abstract concept. It is precisely because the abstract concept does not have a specific concept in the problem field that is used

Abstract classes that characterize abstract concepts cannot be instantiated.

In the Object-Oriented field, abstract classes are mainly used to hide types. We can construct a fixed abstract description of a group of actions,

However, this group of actions can have any possible implementation method. This abstract description is an abstract class, and any possible implementations of this group

All possible Derived classes are displayed. For example, an animal is an abstract class. Humans, monkeys, and tigers are actually derived classes.

The animal type is used to hide human, monkey, and tiger types.


(2) analyze the syntax definition

At the syntax level, the Java language provides different definitions for abstract class and interface. The following describes how to define an abstract class named demo.

You can use abstract class to define a demo abstract class as follows:

Abstract class demo {
Abstract void Method1 ();
Abstract void method2 ();

The following method is used to define the demo abstract class using the interface:

Interface demo {
Void Method1 ();
Void method2 ();

In the abstract class method, the demo can have its own data members or non-abstract member methods. In the implementation of the interface method, the demo can only have

Static data members that cannot be modified (that is, they must be static final, but generally do not define data members in the interface). All member methods are abstract

. In a sense, interface is a special form of abstract class.

From the programming point of view, abstract class and interface can be used to implement the idea of "Design by contract. However, there are some differences in usage.

Abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once (Because Java does not support multiple inheritance-reinjection ). However,

A class can implement multiple interfaces. Maybe this is a compromise between Java designers and Java's support for multi-inheritance.

Secondly, in the definition of abstract class, we can assign the default behavior of the method. However, in the interface definition, the method cannot have default behavior. to bypass this

Restrictions, must use the delegate, but this will increase some complexity, sometimes cause a lot of trouble.

Another serious problem still exists when the default behavior cannot be defined in the abstract class, which may cause maintenance trouble. Because if you want to modify the class interface (

Abstract class or interface representation) to adapt to new situations (for example, adding a new method or adding a new parameter to the used method), it will be very troublesome

, It may take a lot of time (especially when there are many derived classes ). However, if the interface is implemented through abstract class, you may only need to modify the definition.

The default behavior in abstract class can be used.

Similarly, if the default behavior cannot be defined in the abstract class, the same method will appear in every derived class of the abstract class, in violation of "one rule, one place"

Principle, causing code duplication, which is also not conducive to future maintenance. Therefore, be careful when selecting abstract class and interface.

(3) Design Concepts

The preceding section mainly discusses the differences between abstract class and interface from the perspective of syntax definition and programming. The differences between these layers are relatively low-level and non-essential. This section starts

Level: abstract class and interface reflect the design concept to analyze the differences between the two. The author believes that only by analyzing at this level can we understand the essence of the two concepts.


As mentioned above, abstract class represents an inheritance relationship in Java. To make the inheritance relationship reasonable, there must be a "is-a" relationship between the parent class and the derived class,

That is, the concept of the parent class and the derived class should be essentially the same. For an interface, it is not required that the implementer of the interface and the interface definition are consistent in concept.

And implements the interface definition contract. In order to make the discussion easier to understand, we will explain it through a simple example below.

Consider this example. Suppose there is an abstract concept about the door in our problem field. The door has two open and close actions. Now we can use

Abstract class or interface is used to define a type that represents the abstract concept. The definitions are as follows:

Use abstract class to define door:

Abstract class door {
Abstract void open ();
Abstract void close ();

Use the interface method to define the door:

Interface door {
Void open ();
Void close ();

For other specific door types, extends can use the door defined in abstract class or implements to use the door defined in interface mode. It looks like you are using

Abstract class and interface have no major difference.

If you want the door to have the alarm function. How do we design the class structure for this example (in this example, we mainly want to demonstrate abstract class and

Interfaces are reflected in the differences in design concepts. Other irrelevant issues are simplified or ignored )? The following will list possible solutions and address these issues from the design concept layer.

Different solutions are analyzed.

Solution 1:

Add an alarm method to the door definition as follows:

Abstract class door {
Abstract void open ();
Abstract void close ();
Abstract void alarm ();


Interface door {
Void open ();
Void close ();
Void alarm ();

The alarmdoor with alarm function is defined as follows:

Class alarmdoor extends door {
Void open (){...}
Void close (){...}
Void alarm (){...}


Class alarmdoor implements door {
Void open (){...}
Void close (){...}
Void alarm (){...}

This method violates a core principle of ISP (interface segregation principle) in object-oriented design, and inherent behavior of the door concept in the definition of door

The method is mixed with the behavior method of another concept "alarm. One problem is that modules that only rely on the door concept will be caused by the concept of "alarm ".

Change (for example, modifying the parameters of the alarm method.

Solution 2:

Since open, close, and alarm belong to two different concepts, they should be defined in abstract classes that represent these two concepts according to the ISP principle. The two definitions are as follows:

Both concepts are defined in abstract class mode; both concepts are defined in interface mode; one is defined in abstract class mode, and the other is defined in abstract class mode.

Interface mode definition.

Obviously, because the Java language does not support multiple inheritance, both concepts are defined using abstract class. Both of the following methods are feasible,

Their selection reflects the understanding of the concept nature in the problem field, and whether the reflection of the design intent is correct and reasonable. Let's analyze and explain them one by one.

If both concepts are defined using the interface method, there are two problems: 1. We may not understand the problem field clearly. The concept of alarmdoor is essentially true.

Is it door or an alarm? 2. If we have no problem understanding about the problem field, for example, through analysis of the problem field, we find that alarmdoor is essentially a concept and door

As a result, our design intent cannot be correctly revealed during implementation, because the definition of these two concepts (both using the interface method definition) does not reflect the above


If our understanding of the problem field is: alarmdoor is essentially a door, it also has the alarm function. How should we design and implement it to clearly reflect

What do we mean? As mentioned above, abstract class represents an inheritance relation in Java, and the inheritance relation is essentially a "is-a" link. So the concept of door

, We should use the Abstarct class method to define. In addition, alarmdoor has the alarm function, indicating that it can complete the behaviors defined in the alarm concept, so the alarm concept can be

It is defined in interface mode. As follows:

Abstract class door {
Abstract void open ();
Abstract void close ();
Interface alarm {
Void alarm ();
Class alarm door extends door implements alarm {
Void open (){...}
Void close (){...}
Void alarm (){...}

This implementation method can clearly reflect our understanding of the problem field and correctly reveal our design intent. Abstract class indicates the "is-a" relation.

The interface indicates the "like-a" relationship, which can be used as a basis for your selection. Of course, this is based on your understanding of the problem field. For example:

The concept of alarmdoor is essentially an alarm, and it also has the function of door, so the above definition method will be reversed.


(4) abstract class Summary

1. abstract class usually contains one or more abstract methods, which are not implemented. Classes containing abstract methods must be declared as abstract classes. Abstract

All concrete subclasses of the class must provide specific implementation for the super class. If the subclass does not implement the abstract method of the super class, a compilation error will occur unless the subclass is declared as abstract.

2. abstract class declares the common attributes and behaviors of classes in the class hierarchy. Since constructors cannot be inherited, they cannot be declared as abstract methods.

Instantiate the object of an abstract class, but can declare the variable of the abstract type. This variable can be used to reference the object of the subclass.

(5) Summary of interfaces

1. The interface starts with interface and contains a set of abstract methods whose default value is public. The interface can contain variables. The default value is static final, and the initial value must be given.

The current class cannot be redefined or its value cannot be changed. The implementation interface must implement all the methods, the interface cannot have implementation methods, and all member methods are abstract.

2. If a class does not implement any interface method, it is an abstract class and must declare the class with the abstract keyword. Implementing an interface is like reaching an agreement with the compiler,

"I will declare all methods developed by this interface ".

(6) Summary

1. abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces.

2. In abstract class, you can have data members or non-Abstarct member methods. In interface, you can only have static data that cannot be modified.

All member methods are abstract.

3. abstract class and interface have different design concepts. Abstract class represents the "is-a" relation,

The interface represents the "like-a" relationship.


4. The interface is generally used to replace this class when there is no default implementation that can be inherited in the abstract class (that is, there is no instance variable or default method implementation.

5. abstract class is another form of contract, which is the design requirement for implementation, while the interface is the server's requirement for the client.

6. abstract class is a base class and cannot be instantiated. interfaces are declarations. each class of the corresponding interface must implement methods.


7. If a subclass implements an interface, all methods in the interface must be implemented (whether or not required); if it inherits an abstract class, you only need to implement the required methods

This is an advantage of an abstract class.

8. If the method name defined in an interface changes, all subclasses implementing this interface obviously cannot be compiled because the method names they implement do not exist. This is

This problem does not exist if a non-abstract method is modified in the abstract class, but a new method is added to the subclass.

9. Looking at the first two points, it seems that abstract classes have more advantages than interfaces, but they have an insurmountable disadvantage: that is, a subclass can only have one parent class. A extends B.

In this way, a has all the methods and functions of B, but when a still wants to have the functions of C. It cannot be implemented through a extends C, but it requires some detours. Current System Architecture

The trend is to break down functions as much as possible by targeting abstraction (excuses, abstract classes) rather than specific programming. This requires implementation of multiple interfaces. Obviously

Classes cannot provide such functions. From the perspective of system reconstruction, it is very convenient for a specific class to abstract the interface. You only need to write an interface that defines all the methods of a specific class,

Then you can use the implement interface for this specific class. Abstract classes are much more complex, such as B extends a and c extends B. If you want to abstract an abstract for C

Class D, you need to find its top-level A to start from scratch, because it cannot do C extends d

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: 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.