Differences between the interface (interface) and the virtual base class (abstract class) in Java

Source: Internet
Author: User

In the Java language, abstract classes and interface are the two mechanisms that support the definition of an abstraction class. It is the existence of these two mechanisms that gives Java a powerful object-oriented capability. Abstract class and interface are very similar in terms of support for the definition of abstractions, and can even be replaced, so many developers are more likely to choose abstract classes and interface when they are doing the abstraction class definitions. In fact, there is a great difference between the two, for their choice even reflects the nature of the problem areas of understanding, the design intent of the understanding is correct, reasonable.

Abstract class


Instantiation of




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


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

Not private, default is Public,abstract type


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


Need to inherit, to use extends

To use implements

Abstract class and interface are used in the Java language for abstraction classes (the abstract class in this article is not translated from abstract class, it represents an abstract body, while abstract Class is defined as a method for defining abstract classes in the Java language, what are abstract classes and what are the benefits of using abstract classes?

A class that declares the existence of a method and does not implement it is called an abstract class, which is used to create a class that embodies some basic behavior, declares a method for that class, but does not implement the class in that class. An instance of the abstract class cannot be created. However, you can create a variable whose type is an abstract class that points to an instance of a specific subclass. Cannot have abstract constructors or abstract static methods. The subclasses of the abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class.

An interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be obtained by implementing such an interface. All the methods in the interface are abstract, without a program body. An interface can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given by the program body) to all such interfaces. It can then invoke the interface's method on any object that implements the interface's class. Because of an abstract class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will take effect. A reference can be converted to an interface type or converted from an interface type, and the instanceof operator can be used to determine whether the class of an object implements an interface.

Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit entity classes, but only if the entity classes must have explicit constructors. The interface is more concerned with "what functions can be achieved", regardless of "how it is achieved".

1. The same point
A. Both are abstract classes and cannot be instantiated.
B. Interface implementations and ABSTRCT class subclasses must implement an abstract method that has already been declared.

2. Different points
A. Interface need to be implemented, use implements, and abstract class need to inherit, use extends.
B. A class can implement multiple interface, but a class may inherit only one abstract class.
C. Interface emphasizes the implementation of a particular feature, while the abstract class emphasizes its affiliation.
D. Although the subclasses of the interface implementation class and the ABSTRCT class must implement the corresponding abstract methods, the implementations are in different forms. Each method in interface is an abstract method, all just declared (declaration, no method body), the implementation class must be implemented. The subclass of abstract class can be implemented selectively.
This option has a two-point implication:
One is that not all methods in the Abastract class are abstract, only those whose crown has an abstract are abstracted, and subclasses must be implemented. For those that do not have an abstract method, the method body must be defined in the ABSTRCT class.
The second is that when the subclass of abstract class inherits it, it can either inherit directly from the non-abstract method or overwrite it, but the abstract method can be implemented by choice, or it can be implemented by declaring its method abstract, without implementing it and leaving it to its subclasses, but this class must also be declared as an abstract class. Abstract classes, and of course, cannot be instantiated.
E. Abstract class is an intermediary between interface and class.
Interface are completely abstract, can only declare methods, and can only declare pulic methods, cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables. However, interface can declare constant variables, and this example is not hard to find in the JDK. But putting constant variable in interface violates its purpose of being an interface, and it confuses the different value of interface and class. If you really need it, you can put it in the appropriate abstract class or class.
Abstract class plays a connecting role in interface and class. On the one hand, abstract class is abstract and can be used to declare abstractions to standardize the functions that subclasses must implement, on the other hand, it can define the default method body for the subclasses to use or overwrite directly. In addition, it can define its own instance variables for use by subclasses through inheritance.

3. Interface applications
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.

4. Application of abstract class
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.

Differences between the interface (interface) and the virtual base class (abstract class) in Java

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.