Why use Legacy Interface instead?

Asked

Viewed 707 times

2

I would like to know why in java one should usually prioritize the use of interfaces, rather than inheritance with classes.

  • They are different things and therefore used for different purposes. Interface is just to make sure that such class has such properties (typing), herence serves for the same but also passes these properties to the class it will inherit

  • Also useful: https://answall.com/q/152266/101 and https://answall.com/q/87488/101 and https://answall.com/q/86484/101 and https://answall.com/q/107524/101 and https://answall.com/q/2913/101 and https:///pt.stackoverflow.com/q/87423/101 and https://answall.com/q/11378/101 and https://answall.com/q/166530/101 and https://answall.com/q/89236/101 and https://answall.com/q/22718/101 and https://pt.stackoverflowcom/q/81314/101 and http:///pt.stackoverflowcom/q/73449/101 and https://en.stackoverflow.com/q/89894/101

2 answers

6

A good reason is that an inheritance is a much stronger coupling than an interface.

Coupling is when parts of your code are dependent. Imagine that you have a function that calculates the coordinates of a ship on the screen. It is possibly dependent on the java.Math functions to perform the calculations. So it has a certain degree of coupling with java.math. It is a relative concept and that we seek to decrease in our programs, because a high coupling requires that a change in one place generates changes in others. Imagine in this example that java changes the arguments of the sqrt function. Everyone using this function will have to make changes to their code.

Couplings can be caused by composition (an object is composed / has a field of another object), by the use of functions of another object, by the use of interfaces and by inheritance.

Inheritance is considered to contain a high degree of coupling, as anything you do in the parent class will inevitably affect the daughter class. And the daughter class can have access to the "entrails" of the parent class, using the protected fields and methods, so the chance of being reached is greater. The contract between the father and daughter class is more intimate and difficult to define. In other words. If you just "call" a method of an object, you do so because you believe that method will behave in such a way and return a given predicted value. In the case of the parent class, the daughter class has more resources to manipulate parent class data, requiring from the developer more knowledge and increasing the change from generating bugs.

On the other hand, interfaces you do not share fields and methods. Only the signature (the class contract). So the only change that affects the daughter class is the signature change, which language itself will ensure, at compile time, that the daughter classes are revised and corrected.

Another factor related to coupling, but more connected to analysis, is that ideally you should use Java inheritances to implement the concept of inheritance of analysis. That is, in a given domain a student can be a person and it may make sense that the student shares the roles and fields of person. But when using inheritance to reuse algorithms and fields that are not part of the analysis model (for example, the student class will be the father of the course class, because the course needs information and methods to calculate the student’s enrollment), it ends up using a powerful language feature to solve a problem that could have another approach with less coupling.

Finally, when we want to define the boundaries between the application modules, we are defining the components of this application, and interfaces are most suitable for this case.

I hope I’ve helped,

abs

  • Attention should be paid to the detail that in some languages multiple inheritance is not accepted, taking the importance of interfaces.

3

It depends a lot on what you abstract. In cases where standardizing method signatures can be used, the interface can be used, already in cases where if we reuse the attributes of a class and also implement or override methods via polymorphism, it may be better to use inheritance.

Browser other questions tagged

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