Interfaces in Java have the following features
- All the methods defined in interfaces are implicitly abstract even though the abstract modifier is not explicitly declared.
- All the methods in an interface are public by default, whether they are explicitly declared as public or not.
- Variables declared inside an interface are by default public, static, and final. These variables are constants and must be initialized when declared.
- Interfaces cannot be instantiated, which means we cannot create objects of an interface.
- We cannot declare static methods inside an interface. However, from Java 8 onwards, static methods with a body are allowed in interfaces.
- The keyword ‘implements’ is used to implement an interface. A class can implement multiple interfaces by separating them with commas.
- Unlike classes, an interface can extend any number of interfaces. This feature allows for multiple inheritance through interfaces.
- We can define a class inside an interface, and the class acts like an inner class to the interface.
- An interface can extend another interface and also implement an interface at the same time.
- Multiple inheritance in Java is achieved through interfaces. A class can implement multiple interfaces, which allows it to inherit behavior from multiple sources.
Interfaces play an important role in achieving abstraction and defining contracts that classes must adhere to when implementing the interface. They provide a way to achieve loose coupling in Java programs and support the concept of polymorphism, allowing objects of different classes to be treated interchangeably if they implement the same interface.