What is the interface and How it differs from an abstract class?
Any service requirement specification (SRS) is considered as an interface.
From a client point of view, an interface defines the set of services what he is expecting. From service provider point of view, an interface defines the set of services what he is offering. Hence any contract between client and service provider is considered as an interface.
Inside interface, every method is always abstract whether we are declaring or not hence interface is considered as 100% pure abstract class.
Interfaces specify what a class must do and not how. It is the blueprint of the class, It is used to achieve total abstraction.
Any service requirement or any contract between client and service provider or 100% pure abstract class is nothing but the interface.Â
Interfaces have the following properties âˆ’
- An interface is implicitly abstract. You do not need to use theÂ abstract keyword while declaring an interface.
- Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
- Methods in an interface are implicitly public.
Interface declaration and implementation
- Whenever we are implementing an interface for each and every method of that interface we have to provide implementation otherwise we have to declare the class as abstract then next level child class is responsible to provide the implementation.
- Every interface method is always public and abstract whether we are declaring or not hence whenever we are implementing an interface method compulsory we should declare as public otherwise we will get compile time error. Example…