K
K
KickMicro2017-11-14 19:57:49
OOP
KickMicro, 2017-11-14 19:57:49

If it were possible to implement the desired methods in an interface without the need for implementation in descendants, then would abstract classes be needed?

I want to understand the specific purposes of interfaces / abstract classes and what and where can be used

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
MrDywar Pichugin, 2017-11-14
@Dywar

Interfaces or abstract classes

M
Maxim Fedorov, 2017-11-15
@qonand

In order to understand the difference between abstract classes and interfaces, it is worth starting a little away from their implementation for a specific programming language and understanding their essence.
What is an interface? it is a specification of how the outside world interacts with an object. Pay attention to INTERACTIONS, but not implementations.
Let's take for example an ordinary class that describes some kind of switch (i.e., the task of such a class is to turn on or off some device):

public class Switch 
{
    public void On()
    {
        // тут предполагается куча кода для включения устройства
    }
  
    public void Off()
    {
        // тут предполагается куча кода для выключения устройства
    }
}

What is included in this class:
1. IMPLEMENTATION of the device on / off functionality (ie a bunch of code that controls the device)
2. INTERFACE (signature of the On and Off methods), with the help of which class clients interact with it.
You can safely control this switch object based on its interface, knowing that there is an On method responsible for turning it on and an Off method responsible for turning it off. But now imagine that you have dozens of different types of switches with different implementations and a separate developer works on a class for each type. How to make your control algorithm universal and independent of a particular type of switch? To do this, it is necessary that the switch, regardless of its type, has the same interface (roughly speaking, there was an On method and an Off method). But how do developers of switch classes know that you need such an interface? and if they know how to make them comply with it exactly? Agree it would be convenient if there was an opportunity to describe the interface separately somewhere (formalize it), which could be relied upon when interacting with switches, and other developers would simply connect and implement it. This is where a separate interface construction comes to the rescue, which allows this to be achieved. You can do like this:
public interface ISwithable
{
    public void On()
  
    public void Off()
}

this will allow you to work exclusively with the interface, without worrying about specific implementations, as well as third-party developers to strictly follow the rules you need.
Now let's discuss what a class is. As can be seen from the example, a class is an implementation of a specific interface and directly specific functionality. What is the difference between an abstract class and a regular class? the fact that it allows you to make a delayed implementation of a part of the functionality, i.e. shift the responsibility for implementation to their descendants. But nevertheless, it still not only implements the interface but describes the functionality. Of course, using an abstract class, you can make some kind of imitation of only the interface by making all methods abstract, but this is not entirely correct. The tasks of an abstract class are, first of all, the creation of possibilities for the deferred implementation of specific functionality.

G
Georgy Pelageykin, 2017-11-15
@ArXen42

In general, detailed answers have already been given, I once remembered the following statement on this issue:

  • An interface describes what an object can do.
  • An abstract class describes what an object is .

A
Alexander Yudakov, 2017-11-15
@AlexanderYudakov

And in Kotlin there is an opportunity for some interface methods to specify the default implementation :)
kotlinlang.ru/docs/reference/interfaces.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question