What is the criterion of Java in having in some cases, a class in its API and in other cases an interface?

Asked

Viewed 58 times

0

The Java API is immense, but some pre-defined features in it are implemented through a class and other functionality through an interface.

What criterion used by Java to choose one or the other?

  • It has to do with the coupling¹,²

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

5

Basically the same criterion that anyone should adopt when creating a Java application or the criterion to be adopted in another language, this concept is universal and should not depend on technology.

Of course there may be a certain subjectivity in the choice, but between these two things it is a matter of fitting more with the goal, of being naturally one thing or the other.

The criterion could be more specific if it was just about Java to choose whether something will enter your default API.

So first you need to understand what each thing is, so it’s easy to define, that is, they choose it because they know what each mechanism is for and they know what they’re doing specifically and that it will be more appropriate to use one or the other.

It may even be better something else. And today interfaces are more modern. And soon there will be "classes" by value, which are not considered classes anymore, and are already starting to have the classes pre-implemented (the Records). So the decision gets harder, but I’m not going to talk about these mechanisms that are not in the question.

For now, learn:

That’s just the tip of the iceberg to help choose, you have to understand everything about computing, how to do the design code.

So, in short, interfaces are used to define contracts what other types will have, are to define what capabilities an object will have and that the code will be able to access it "securely", while the class defines an object, what can include interfaces. You need to see what you are doing there, is to define an object or contract what objects will have? That’s the basic question to ask.

Browser other questions tagged

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