I
I
Ilya Temnikov2022-01-08 02:02:13
Java
Ilya Temnikov, 2022-01-08 02:02:13

Why do interfaces have logic?

I thought about this question today. In the Java programming language, there are such concepts as an abstract class and an interface. They are very similar entities with a few exceptions.

In Java before version 8, the differences are as follows: an abstract class is primarily a class, which means it does not support multiple inheritance. But interfaces can be implemented endlessly. In an abstract class, absolutely any state can be encapsulated, but an interface cannot afford this. In an abstract class, you can set default logic, but not in an interface. These are perhaps the key differences.

Everything is logical. Here's an abstract class, here's an interface. An abstract class is like a template for non-defined objects, but an interface is like a contract about what the class should do.

Since version 8 of the PL, the ability to set states and add logic to methods has been added to interfaces.
The question is, how do these two entities differ now? And how to understand where to use the abstract class, and where the interface?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikolay Savelyev, 2022-01-08
@AgentSmith

And how to understand where to use the abstract class, and where the interface?

Just like before. An interface is just a contract, as rightly said.
You never know what they did there in Java8, the concept of OOP should not suffer because of them.
Well, classic, follow the principles of SOLID, regardless of the features of some PL, whatever they may be.
This is a holivar topic, there is a discussion here
https://stackoverflow.com/questions/19998454/when-...

J
Jacen11, 2022-01-08
@Jacen11

added the ability to set states and add logic to methods in interfaces
I have never experienced this in real life. Theoretically, it can simplify development. In the interface, we describe the capabilities of objects, and if different classes can have one method implementation, why not write it right away in the interface? But it’s hard for me to think of a case where it will be really useful

M
Michael, 2022-01-18
@Akela_wolf

1. A class can have fields (state), no interface. Here is the first and main difference
2. An interface cannot have protected and private methods, a class can.
In general, you can write methods with implementation in interfaces in order to document directly in the interface that "this method is, as it were, not an independent method, it is implemented on top of other interface methods. But it is often used, it is convenient, so here - keep it right in the interface" . Well, if necessary, this method can be overridden in the class, for example, to provide an optimized version.
Also take a look at extension functions in Kotlin - they come up with the same idea, although they have their advantages (easy to add to an existing interface / class) and disadvantages (cannot be overridden in a derived class)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question