A
A
Artyom Smirnov2016-11-13 17:34:51
Java
Artyom Smirnov, 2016-11-13 17:34:51

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

What is the difference between an abstract class and an interface in Java? And in what situations is it better to use an abstract class, and in which - an interface?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
Sergey, 2016-11-13
@ArtFutureDev

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

Everything rests on the concept of "type". In the old days, that is, in the days of the Simula language, from which the creators of C ++ drew inspiration, there were only classes. And the type system was based on classes. Moreover, the inheritance mechanism was implemented as it was implemented, solely to save memory, which was very expensive in those days.
In order to achieve polymorphism, we must be able to declare abstract types. Like "any crap that has this type will work as it should." This is why abstract classes appeared in languages ​​like C++. Since sometimes we want to do composition of abstract types, C++ implemented multiple inheritance.
Java, which draws much of its inspiration from C++ and smalltalk, decided to introduce another entity - interfaces. It was kind of a simplified way to define an abstract base type. As a result, in order not to solve the problem of a diamond (or rhombus), it was decided to abandon multiple inheritance and allow classes to implement several interfaces.
Due to this, we get the opportunity to do the composition of types as we want. That is, the whole difference comes down to the fact that when inheriting from an abstract class, we inherit classes, while interfaces allow our classes to implement abstract types.
In general, abstract classes are needed when you need inheritance. This is usually in situations where you have several classes that must have a common abstract type (that is, you cannot select the weakest ancestor in terms of constraints). For example, if we make a chain of classes String <- Email, then there is no point in abstract classes, since the String type already includes a subset of Email types.
In general, java8 has already introduced the ability for interfaces to have a base implementation, so I won’t be surprised if, over time, the extends keyword is abandoned in principle, getting rid of the extra entity.
I also recommend reading: www.javaworld.com/article/2073649/core-java/why-ex...

D
Dark Hole, 2016-11-13
@abyrkov

To put it "easier than Wikipedia," the main difference between an abstract class and an interface is that interfaces can be inherited multiple times, while abstract classes can contain non-abstract things.
As for their use ... everything is much simpler here if you understand why you need an abstract class, and why you need an interface. An abstract class is used when we want a concrete implementation, but it needs to be flexible. The interface is used so that the implementation of this thing exists at all.

K
Konstantin Stepanov, 2016-11-13
@koronabora

Interfaces are used for multiple inheritance. You can't write method implementations in an interface. With these restrictions, we eliminate one of the problems of multiple inheritance: if we inherit from 2 classes with the same method (this is possible in C ++), then by calling this method in the heir, it is not clear which implementation to use. Inheriting from several interfaces, we write an implementation specifically for our class, and this problem is eliminated.

S
sitev_ru, 2016-11-14
@sitev_ru

Everything is already on Wikipedia:

An interface is simply a pure abstract class, that is, a class that does not define anything other than abstract methods.

D
dev400, 2016-11-14
@dev400

An abstract class contains abstract methods with common functionality that will be logical for the methods of its descendants (see aside Barbara Liskow's substitution principle). The interface simply describes the possibilities.

P
pazukdev, 2021-01-10
@pazukdev

1. Interface is the contract of the system with the external environment. More Java-specific: a contract for a class to perform certain behavior, implemented in a form resembling a fully abstract class.
Apply always. This is the main way to implement abstraction.
2. Abstract class is a template for a group of cat classes. cannot be instantiated. does not contain a complete description of the behavior (may contain partial).
Apply only in specific cases when you need some kind of state in abstraction.
Specific differences can be found here: https://ru.stackoverflow.com/a/1229336/258227

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question