what is the difference between required and Provided interfaces

17,879

Solution 1

Provided and required interface always refer to the concept of interface, indicating the point of view.

I hope the following diagrams sheds some light on the subject.

enter image description here

On the implementation level a provided interface is the interface implemented by a class (in the most common sense, e.g. a class B implements the interface I). Required interface would be any use of an interface by a component (e.g. if a class A defines a method that has the interface I as a parameter, this means that class A has a required interface I).

Solution 2

I think that you are confusing interface in general sense and language specific construct that is also called interface.

In general sense interface means point of intreaction between two parts/objects/system. At very low level, you can say that all public members (methods + fields) of an object compose its intreface.

At higher abstraction level programmers often think about API as interface for library/system. But that does not mean that this API consist of just one Java interface. The API contains all objects, methods, contsructors, config files... that are ment to be used by users of the library.That is probably what is ment by your required and provided interfaces.

If you write java libary, you usually require the API of Java standard library (everything in java package) - that would be the required interface. (it can be provided by JVM of any implementation, for example Android is using the same interface as Java but it is not java) On the other end your library would also expose some interface - the way people could use your library - that would be called the provided interface. (again if I say interface I don't mean one java interface, it would be probably mix of several interfaces + implementations + some value classes)

One other term you might encounter is SPI Service Provider Interface which is similar to API, but the users of SPI don't make calls to this interface, but rather implement it and expose it back to the original system. It's a way to describe interface for plugins.

Solution 3

Required and provided interfaces appear to be UML-related terms, where a provided interface describes functionality offered by a class and required interfaces describe functionality needed by another class: further reading.

In Java, all interfaces are the same; there is no distinction between provided/required.

Previous link no longer work, but https://www.ibm.com/developerworks/rational/library/dec04/bell/index.html can be of help

Share:
17,879

Related videos on Youtube

Micha agus
Author by

Micha agus

Updated on September 14, 2022

Comments

  • Micha agus
    Micha agus over 1 year

    I know in general :

    an interface is a reference type, it is similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces ?

    But what is the difference between required and Provided interfaces ?

    • Admin
      Admin almost 10 years
      Where did you read about "required and Provided interfaces"?
  • user5193682
    user5193682 almost 8 years
    I see... so the focus in required is to show that a class cannot work properly if the interface is not imemented. Its a matter of life or death for it. For the class who is providing it, it could 'survive' without it, but is providing it anyeay just to be 'nice' to another class. If this the correct, could you make it more clear in your,explanation? Maybe using a more clear example: one starving guy that will die if not food is given to him, and a guy who doesnt need for for now, but will do the extra work of helping the first guy anyway
  • M.Ionut
    M.Ionut almost 3 years
    a picture is worth a thousand words. +1 for providing it and also explaining what "provided" & "required" means at implementation level. Thanks!
  • Farruh Habibullaev
    Farruh Habibullaev over 2 years
    Simple and to the point!