Interface is very important aspect of programming and designing an application in Java. I would like to list 10 points which is very important when we consider an interface for programming.
1. Interfaces are like contracts for the class which implements it. Class need to adhere this contract. But interface do not force how this contract or the functionality is to be implemented.
2. In interface we can have constants. But these constant are always public, final and static implicitly.
3. By default, method inside an interface are public and abstract , but user can have explicit declaration if he wants. Interface method cannot be static. As interface methods are abstract, these methods cannot be marked with strictfp, native and final.
4. An Interface is like pure abstract class, but interface allows only abstract methods not concrete methods.
5. When we say contract lets see how the class ( nonabstract) which is implementing an interface should follow the contract :
1. It should provide concrete implementation of the interface methods.
2. It must not declare new checked exceptions for the implemented method, but runtime exception allowed.
3. Broader exception declaration in implemented method is not allowed.
4. Same signature of an interface method should be maintained in implemented method.
6. Interface can extend one or more interfaces.
7. An interface must be declared with keyword interface.
8. An Interface cannot implement or extend any class.
9. An interface provides multiple inheritance as class can implement many interfaces unlike a class which can extend only one class.
10. A class which implements an interface can be abstract i.e this class do not need to implement the methods which are provided in an interface, but first subclass which extend this abstract class must implement the methods.
- Blogger Comment
- Facebook Comment