Interface in Java
Collection of abstract methods in java is called Interface. The interface is used to achieve abstraction in which you can define methods without their implementations. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Declaring and Implementing an Interface in Java
The interface keyword is used to declare an interface and Implements keyword is used to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
Example for Declaration of a Java Interface

Example for Implementing a java Interface

Implementing a java Interface
Some of the key points on Interface.
1. Abstract Methods: An interface can contain abstract methods (methods without a body). Any class that implements the interface must provide concrete implementations for these methods.
Example:

2. Default Methods: Interfaces can also have default methods with a body. These methods have a default implementation and can be optionally overridden by implementing classes.
Example:

3. Static Methods: Java interfaces can also have static methods. These methods belong to the interface and cannot be overridden by implementing classes.
Example:

4. Multiple Inheritance: A class can implement multiple interfaces, which is Java's way of providing multiple inheritance.
Example:

5. No Constructors: Interfaces cannot have constructors because they cannot be instantiated on their own. You can only instantiate a class that implements the interface.
6. Implementing an Interface: A class uses the implements keyword to implement an interface. If a class implements an interface, it must provide implementations for all the abstract methods of the interface, unless the class is abstract.
Example:

Why do we need interface in Java?
· Java interfaces define method signatures without implementations, giving classes a template to follow.
· It's a way to achieve multiple inheritance, as a class can implement multiple interfaces.
· They encourage code flexibility, which facilitates scalability and easier maintenance.
Abstract class vs Interface
Abstract Class | Interface |
Can have both abstract and concrete methods | Can have abstract methods, default and static methods. |
Can have final, non-final, static, and non-static variables. | Only static and final variables. |
Can extend only one class (abstract or not) | A class can implement multiple interfaces |
Can have constructors | Cannot have constructors |
Can provide the implementation of interface methods | Cannot provide the implementation of abstract class methods |


