Why do some APIs provide mostly interfaces and not classes?

Some Java APIs provide a large number of interfaces and several classes. For example, the Stellent / Oracle UCM API consists of approximately 80% interfaces / 20% classes, and many of the classes are just exceptions.

What is the technical reason for preferring interfaces over classes? Is this just an attempt to minimize grip? Improve encapsulation / information hiding? Something else?

+2


a source to share


5 answers


It would be to maximize their flexibility in changing base classes behind the scenes.



As long as the interfaces / contracts remain the same, they can change the implementation classes they want without worrying about impacting the people who use their library.

+9


a source


They are intended for third parties to ensure implementation.

A classic and successful example is JDBC (22 interfaces 7 concrete classes)



The idea is to provide .... a programmer's interface (API) so that clients (those using the code) can freely pass to the functions that the API provides without worrying about the underlying implementation.

Another reason is that there might be an existing provider (i.e. FutureSQL) that still doesn't exist, but it can implement these interfaces and you can use it.

+3


a source


The framework / API was probably designed with Dependency Injection , extensibility, low coupling and high cohesion

0


a source


The number of interfaces relative to the number of classes is not so important if each class implements a large number of interfaces.

0


a source


One of the best examples is the java.sql package.

The reason is this: when the designer has a clear idea of ​​what needs to be done and how the entire application [not yet developed] is structured, they expose that idea through the API as a bunch of interfaces.

For example: when SUN (now oracle :-() publishes an API for JDBC, the entire SQL package (well, most of it) is just interoperable interfaces that are very well defined, but the actual DB / RDBMS provider knows what to do to they achieve the expected results in the API.

Hence, you can write ur java and DB interaction separately, while the database provider writes the database separately. The vendor just has to write a driver that conforms to the API (s) standard without telling you how they did it.

However, ur and DB app will deal with any problem [and most of the time; -)]

This is a long answer, but hope it helps. Thanks,

Ayusman

0


a source







All Articles