What is an Interface? Explain with realtime use case? (java)
Interface: as name suggests, it is an interface between your class and out side world.
interfaces can contain only constants, method declarations, and nested types. It can't contain method definitions.
Just like an abstract class, you can't create object for an interface.
You can imagine interface just like a header file in c program. It tells what all the functionalities supported by your interface.
Use cases of Interfaces:
1. It can be used as APIs for third party libraries or server side libraries when using with client application. This is the main use case.
Eg: If you are selling some libraries to some clients, then you will not expose the code. Rather you give in terms of .lib format.
But since lib is unreadable format, you have to create an interface file telling what all the functions available in that library.
Eg: Similarly if you want to access some server side functionalities in your client application, then also we use interfaces with some libraries.
2. You can use interfaces to implement multiple inheritance.
Back To Top