What are the differences between abstract classes and interfaces in Java?

Source: Internet
Author: User

Simply put, the interface is public, there can be no private method or variable, is used to let others use, and abstract class can have private methods or private variables,
In addition, the implementation of the interface must be implemented in all the methods defined in the interface, and implementation of the abstract class can selectively rewrite the need to use the method,
In general applications, the top-level is the interface, and then the abstract class implementation interface, finally to the specific class implementation.
Also, interfaces can implement multiple inheritance, and a class can inherit only one superclass.
However, you can implement multiple inheritance by inheriting multiple interfaces,
The interface also has the function of identifying (there are no methods, such as remote interfaces) and data sharing (the variables inside are all constants).

Interfaces and abstract classes are the upper layers of the inheritance tree, and their common denominator is as follows:

1) are the upper layers of abstraction.
2) can not be instantiated
3) can contain abstract methods, which are used to describe the functionality of a class, but are no more than a concrete implementation.

Their difference is as follows:

1) In abstract classes can write non-abstract methods, so as to avoid repeating them in subclasses, which can improve the reusability of the code, which is the advantage of abstract classes, there can be only abstract methods in the interface.

2) A class can inherit only a direct parent class, which can be either a concrete class or an abstract class, but a class may implement multiple interfaces.

The inheritance of classes in the Java language is a single-inheritance reason:

The JVM uses a different binding rule when the subclass overrides the parent class method, or hides the parent class's member variables and static methods.

If a class has multiple direct parent classes, the binding rules become more complex. To simplify the architecture and binding mechanism of the software, the Java language prohibits multiple inheritance.

Interfaces can inherit multiple, because there are only abstract methods in the interface, no static methods, and very few properties.

Only the implementation class of the interface overrides the method in the interface. Therefore, a class with multiple interfaces does not increase the binding mechanism and complexity of the JVM.

For an existing inheritance tree, it is convenient to abstract the new interface from the class, but it is not easy to abstract the new abstract class from the class, so the interface is more advantageous to the maintenance and reconstruction of the software system.


This is the problem that beginners often encounter, see this problem, oneself think up before painful learning process.

Just a quick answer, please. Is there a difference between an interface and an abstract class? Can be sure of the answer: there are differences.

And since there is a difference, we might as well explore with questions why language designers design interfaces and abstract classes in such a way as to differentiate their purpose.

Programming language design is actually a philosophy.

First, interfaces and abstract classes are designed to be different. An interface is an abstraction of an action, and an abstract class is an abstraction of the source.


For abstract classes, such as men and women, we can design a higher level abstract class for these two classes-people, for example, two classes.

For the interface, we can sit down to eat, can stand to eat, can eat with chopsticks, can eat with a fork, and even learn Sango the same with the hands of the food, then you can take these meals to abstract into an interface-eat.

So in high-level languages (such as java,c#), a class can inherit only one abstract class (because you can't be both a creature and a non-living creature).

But a class can implement multiple interfaces at the same time, such as the driving interface, ice skating interface, PA PA interface, football interface, swimming interface.

To summarize a few words:

1, abstract classes and interfaces can not be directly instantiated, if the two to instantiate, it involves polymorphism, do not understand polymorphic see my answer to the Java polymorphism with a few words can be interpreted intuitively? -Know.

If an abstract class is to be instantiated, the variable defined by the abstract class must point to a subclass object that inherits the abstract class and implements all the abstract methods of the abstract class.

If the interface is to be instantiated, the variable defined by this interface points to a subclass object, which must implement all the methods of this interface.

2, abstract class to Quilt class inheritance, interface to quilt class implementation.

3, the interface can only be declared method, the abstract class can either declare the method or the method can be implemented.

4, abstract class inside the abstract method must be all quilt class implementation, if the subclass can not be fully implemented, then the subclass must also be an abstract class. The methods inside the interface must also be implemented by the quilt class, if the subclass cannot be implemented then the subclass must be an abstract class.

5, the interface inside the method can only declare, cannot have the concrete realization. This shows that the interface is the result of the design, and the abstract class is the result of the refactoring.

6, abstract class inside can have no abstract method.

7, if there is an abstract method inside a class, then this class must be an abstract class.

8, the methods in the abstract class are to be implemented, so the abstract method can not be static statics, nor can it be private.

9, the interface (class) can inherit the interface, and even can inherit multiple interfaces. However, a class can inherit only one class.

10. Abstract level (high to Low): interface > Abstract class > Implementation class.

11, abstract class is mainly used for abstract categories, the interface is mainly used to abstract method function. When you focus on the nature of things, use abstract classes, and when you focus on an operation, use an interface.

12, the function of abstract class should be much more than interface, but the cost of defining abstract class is higher.

Because a class in a high-level language can inherit only one parent class, you must abstract the common properties and methods of all subclasses of this class when designing the class.

However, a class (interface) can inherit multiple interfaces, so you only need to abstract the specific action method to this interface for each interface.

In other words, the design of the interface is more extensible, and the design of the abstract class must be very cautious.


In practice, using abstract classes, or inheritance, is a strongly coupled design used to describe the relationship of "A is a B", that is, if a inherits from B, then the use of a as B in the process should be completely free of problems.

For example, in Android, various controls can be treated as view.

If you have two types of relationships in your design that are not "is a", but "are like a", you must consider inheritance carefully.

For example, the live stream controller (livestreamcontroller) and video controller (Cameravideocontroller) in a frame I saw two days ago,

Although their life cycle is very similar: Accept the interface control, have the start, stop, pause and other state functions.

But it is clear that they have the resources and responsibility of the functions are completely different, if this time just because it looks like to get a "Controller" base class to deal with, can only be to find trouble for themselves.

These two types are obviously a combination of relationships, that is, Livestreamcontroller holds Cameravideocontroller objects, which control the state of Cameravideocontroller according to the instructions from the interface.

Interfaces, however, are used to describe the behavior common among objects of various types, indicating that "all objects that implement this interface can do so".

Very typical is the various client programming inside the button click Listen (Onclicklistener,touchlistener, etc.),

This interface, which can be implemented by any type, means that "objects of this class can handle button click events",

As to whether it is an activity, a fragment, or an anonymous inner class, for a view that holds a reference, there is no relationship of a penny,

It executes the onclick () method that implements the Onclicklistener XXXX class object whenever the Click event is triggered, and then executes the implementation into the OnClick () method.

In other words, all objects that implement the Onclicklistener class can have the view "OnClick";

Similarly, for example, when we write the Application Network layer framework, all the objects that implement the Requestcallback (their own callback interface) can make the request "Callback", which is called "Common behavior".

    • All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
    • A class can implement many interfaces, but only one abstract class is inherited
    • Class if you want to implement an interface, it must implement all the methods that the interface declares. However, a class can not implement all methods of an abstract class declaration, and of course, in this case, the class must also be declared abstract.
    • An abstract class can implement an interface without providing an interface method implementation.
    • The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.
    • The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
    • An interface is absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be called if it contains the main method.

The design purpose of itself is different.

I have always believed that the knowledge of engineering has a very obvious characteristic: "Use-oriented". In discussing the differences between interfaces and abstract classes, I would like to try to summarize the differences from the perspective of "use", so I think about the design purpose.

The purpose of the interface is to constrain the behavior of the class (more precisely, a "yes" constraint, because the interface cannot specify what the class cannot do), that is, to provide a mechanism that enforces the requirement that different classes have the same behavior.

It only constrains the behavior, but does not limit how the behavior is implemented. I think the "interface is a constraint" understanding, I feel that with the generic edible effect is better.

Abstract classes are designed to reuse code.

When different classes have some of the same behavior (recorded as behavior set a), and some of the behaviors are implemented in the same way (a non-true subset of A, which is recorded as B), you can make these classes derived from an abstract class.

The implementation of B in this abstract class avoids having all subclasses implement B, which achieves the purpose of code reuse.

The part of a minus B is left to the individual subclasses to implement. Because the A-B is not implemented here, an abstract class is not allowed to instantiate (otherwise it cannot be executed when called to A-B).

1. All methods of an interface must be abstract methods, and abstract classes can have non-abstract methods

2. All methods and properties of an interface are public, and abstract classes can have private properties and methods

3. Common class if you want to implement an interface, you must implement all the methods of the interface, but you can define the normal class as an abstract class to selectively override the interface method.

4. All methods of interface must be implemented for ordinary classes, and abstract methods of abstract classes must be implemented, and non-abstract methods can not be implemented.

5. Ordinary classes can implement multiple inheritance on an interface, only one for an abstract class, but an abstract class can inherit multiple interfaces to implement multiple inheritance


The difference has been said: The main thing is that Java cannot inherit more, but can implement multiple interfaces.

Other secondary such as abstract class can have variables, interfaces can not have Ah, interface methods are all public, abstract class can be private ah.

Remember the main difference is the line, in fact, you write a year code will not write an abstract class, interface will write a lot.

The rotten example is: The Swan inherits the birds. Swans are the children of birds, swans can fly, flying is a common property that can be taken out, such as bees can also fly. This may be the difference in meaning.

Aside from the multiple inheritance, this two goods realistic difference is not imagined large.

The appearance of object-oriented language is mainly to solve the problem of reuse and encapsulation.

It's just that the mind is slowly overstating it. The Java language Designer is an object-oriented sect, and is very persistent with the object-oriented theory of completeness (in addition to a few basic types in Java, what inherits object is not?).

If the high version is not for forward compatibility, the base type is also replaced by the package class earlier.

Object-oriented also sub-genre, the last century, America several masters for can inherit dozen of the bloody, n banner articles, each other can not see.

But just now Google a bit, it is difficult to find. But later, the concept of "interface-oriented programming" was gradually being received by more people.

Because the more you write large programs, the more programmers write code every day, the more the protocol is more important than the hierarchy, and where there are so many subclasses of the parent class, the interface is essentially a protocol.

In fact, Java's own class library is also more and more use of interfaces.

One of the most common examples: HashTable, the original is to implement the dictionary this abstract class. He was replaced by HashMap.

and HashMap corresponding to the original function is to implement the map interface to complete (of course, want to thread synchronization can be used Concurenthashmap, or sync a bit).

Each language has a design idea of each language, in fact, the different, take the inheritance to say:

In fact, C + + support multiple inheritance (this is also Java spit slot), so many C + + code, also did not see how much trouble to the programmer to bring much pain, python more inheritance, but also live good, how many people love it.

Many modern languages, such as swift and extension, dynamically put a new approach to a class or struct, and are accepted by many programmers.

Java does not support multiple inheritance, and there are sealed keywords that specifically prevent inheritance, but there are also many fans.

Language design philosophy is different, out of the language is much worse, and these differences are the result of deep thinking.

The popular language of living is bound to have its uniqueness.

What are the differences between abstract classes and interfaces 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.