When to use Interfaces

Asked

Viewed 3,061 times

10

I always had the following question: When should I really use an interface instead of inheritance and what advantages can I get ?

1 answer

15


Interfaces

An interface is a contract: the guy writing the interface says:

"Hey, I accept things this way here." And the guy using the interface says:

"Okay, the class I’ll write will be like this".

An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. However, starting from Java 8 this is no longer true with the default methods. The interface can’t do anything. It’s just a pattern.

For example (pseudo code):

//Eu digo todos os veículos a motor deve ser semelhante a este:

Interface MotorVehicle
{
     void run (); 
     int getFuel();
}

//Então minha equipe cumpre o "contrato" e escreve um veículo de acordo com o especificado

classe Car implements MotorVehicle
{
     int fuel;
     void run ()
     {
         print ("Wrroooooooom");
     }

     int getFuel ()
     {
         return this.fuel;
     }

}

Perks

It separates the "contract" from the implementation. You have a set of pure methods that you can call without any knowledge about their implementation.

Ensures that all inherited methods can be safely called

It is easy to implement Apis using interfaces, so all interface implementations provide the methods provided in each class.

Disadvantages

From my point of view the disadvantages of Interfaces would be more with respect to its misuse. For example, in Java it is customary to create interfaces for everything, often unnecessarily. It’s as if the developer signed several contracts without reading and then has twice as much work to do maintenance on the system. Another incorrect use is to put many methods in a single interface. This violates the SOLID principle of the segregation of interfaces, which makes it virtually useless for reuse. And follows this line.

Inheritance

Inheritance encourages the use of classes. You can extend the functionality of an existing class either within the same project or in another project. With inheritance, you can extend functions, Features, etc. from an existing class to a new class.

class Pessoa{
    String titulo;
    String nome;
    int idade;
}

classe Funcionario: Pessoa{
    int salario;
    String titulo;
}

The official inherits from Pessoa

Perks:

One of the key benefits of inheritance is minimizing the amount of duplicate code in an application, by sharing common code across several subclasses. But the overriding recommendation for code reuse is to create specialized classes and delegate. Reusing code with inheritance increases coupling unnecessarily in most cases. Where there is equivalent code in two related classes, the hierarchy can usually be reworked to move the common code to a superclass. This also tends to result in better code organization.

Inheritance can also make application code more flexible to changes because classes that inherit a common superclass can be used alternately. If the return type of a method is superclass

Reuse - ease of using base class public methods without rewriting the same extensibility - extending base class logic as per business logic class Derived data - class base may decide to keep some data private so that it cannot be changed by the derived class

Replacing - With inheritance, we will be able to replace the base class methods, so that the significant implementation of the base class method can be designed in the derived class.

Disadvantages

One of the main disadvantages of inheritance is the increased time/effort that takes the program to "jump" through all levels of overloaded classes. If a given class has ten levels of abstraction above it, then it will essentially take ten jumps to perform through a function defined in each of these classes

The main disadvantage of using inheritance is that the two classes (base and inherited class) are strongly coupled. This means that they cannot be used independently of each other.

In addition, over time, during maintenance the addition of new functionalities both basic as well as derivative classes must be changed. Whether the signature of the method is changed in both cases (inheritance and composition)

If a method is eliminated in the "super class" or aggregate, then we will have to refactor it in case of using methods. Things can get a little complicated in the case of inheritance, because our programs will still compile, but subclass methods will no longer override superclass methods. These methods will become independent methods in their own domain.

  • 2

    From my point of view the disadvantages of Interfaces would be more with respect to its misuse. For example, in Java it is customary to create interfaces for everything, often unnecessarily. It’s as if the developer signed several contracts without reading and then has twice as much work to do maintenance on the system. Another incorrect use is to put many methods in a single interface. This violates the principle SOLID segregation of interfaces, which makes it virtually useless for reuse. And it follows this line. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.