
1. Overview
In this article, we will learn how to extend an Interface in Java. To learn more about Java, refer to our articles.
2. Java Interface
An Interface is a contract that the classes must follow. The classes which implement the interface would provide their own implementations of the abstract methods without violating the contract.
An Interface can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. We cannot instantiate interfaces.
Only the classes can implement the interface. However, it can be extended by other interfaces.
A class that implements an interface must implement all the methods declared in the interface.
2.1. How to extend an interface in Java
An interface can extend many interfaces by providing a comma-separated list of parent interfaces, and the interface body.
For example, the following ChildInterface
extends three interfaces Interface1
, Interface2
and Interface3
. It also adds its own contract.
public interface ChildInterface extends Interface1, Interface2, Interface3 { ... ... }
The public
access specifier shows that any class can use the interface in any package. If you do not specify that the interface is public
, then your interface is accessible only to classes defined in the same package as the interface.
An interface can contain constant declarations. All constant values defined in an interface are implicitly public
, static
, and final
. So you can omit these modifiers.
4. Conclusion
To sum up, we have learned to extend an interface in Java.