When using Interfaces

7

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

    
asked by anonymous 08.05.2015 / 06:25

1 answer

12

Interfaces

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

"Hey, I'll take 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, from Java 8 this is no longer true with default methods. The interface can not 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;
     }

}

Advantages

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

Ensures that all methods that inherit it can be called safely

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

Disadvantages

From my point of view the disadvantages of Interfaces would be more in relation to their 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 double the work to do maintenance on the system. Another misuse is putting many methods into a single interface. This violates the SOLID principle of segregation of interfaces, which makes it practically useless for reuse. And follow that 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 employee inherits from Pessoa

Advantages:

One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code across multiple subclasses. But the primary recommendation for code reuse is to create specialized classes and delegate. Reapping code with inheritance increases the coupling unnecessarily in most cases. Where equivalent code exists in two related classes, the hierarchy can usually be reformulated 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 interchangeably. If the return type of a method is superclass

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

Overriding - With inheritance, we will be able to override the base class methods, so that meaningful implementation of the base class method can be conceived in the derived class.

Disadvantages

One of the main disadvantages of inheritance is the increased time / effort that drives 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 execute through a function defined in each of those classes

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

In addition, over time, during maintenance the addition of new functions both basic as well as derived classes should be changed. If the method signature is changed in both cases (inheritance and composition)

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

    
08.05.2015 / 06:56