Search code examples
javaoopinterfacepublic

Interfaces VS APIs VS Public classes


I am a little bit confused about the meaning and the differences between these three terms.

  1. Interfaces
  2. APIs
  3. Public classes

I asked before if there is any difference between APIs and Public classes. Here is the question: Public methods vs public APIs

I got it, but now with the new concept Interface, I got confused again about the differences between Interface and API and Public class, so to make things clearer, let's see this sentence:

Information hiding allows developers to work on modules separately, without needing other developers to know the implementation details of the module they are working on. The module is instead used through its interface.

What is the meaning of interface here? Is it the same as public class?


Solution

  • A big reason these terms are confusing is because they are keywords in Java and they have meaning in a general, language-agnostic context. What the terms mean in the general context of programming is slightly different from what the specific keywords mean in Java.

    In general, the term interface is synonymous with abstraction. When you read, "Program to an interface" it means program to an abstraction. In Java, the interface keyword indicates a specific type of stateless abstraction, which is contrasted with abstract class. In general, both of these are abstractions and thus both are programming interfaces.

    In general, the term public indicates any part of code that is visible to and used by clients outside of the development team. In Java, the public keyword indicates a specific access level between classes and members. Since clients may view and use protected members as well, these may also be considered public in the general context of programming. Effective Java uses the term exported to combine public and protected members. Note that in Java 9+ modules can be used to control access, so even public classes may not be exported and thus may not be public in the general sense.

    The term API is not a Java keyword, so at least we have no language-specific ambiguity to contend with. It is typically used to mean public API, which is synonymous with exported API. It is the sum of everything that you (the developer) allow your clients to view and use. Occasionally, the term internal API is used to indicate the opposite: classes and members that you do not allow your clients to view or use.