Java : Interface
In this article, we will explore the fundamental concepts of interfaces in Java, understand their purpose and properties, examine their relationship with classes, and look at practical examples of how to create and use them through practical examples.
What are Interfaces?
In Java, an interface is a contract or blueprint that specifies a set of requirements. Any class that implements an interface must fulfill the requirements defined by the interface. Interfaces provide a way to achieve abstraction and multiple inheritance in Java. An interface can contain both fields & methods, By default fields are public, static and final and methods are public and abstract.
interface Deck {
void shuffle();
}In the example above, the Deck interface provides an abstract method void shuffle() — It has no implementation. Any class that implements an interface must provide an implementation for all its abstract methods. Otherwise, the class must itself be declared abstract.
It is not necessary to explicitly declare methods in an interface as
publicorabstract, these modifiers are implied. Similarly, It is not necessary to explicitly declare fields in an interface aspublic,static&final.
Properties of Interface
- Interfaces are not classes. They are abstract types, Hence, you cannot instantiate an interface using the
newkeyword. - You can create object references of interface type, but they must refer to objects of classes that implement the interface.
- Fields in an interface are implicitly
public,static, andfinal. - Methods in an interface are implicitly
publicandabstract, unless declared otherwise. - Interfaces can contain
staticmethods. - Interfaces can contain
privatemethods. These are non-abstract and can be used only withindefaultorstaticmethods. - Interfaces can have
defaultmethods with an implementation, introduced in Java 8 to support Interface Evolution.
Based on the properties describe above an generalized example of an interface is provided below.
interface InterfaceName {
// Constant field (implicitly public, static, and final)
int SOME_CONSTANT_FIELD = 10;
// Abstract method (implicitly public and abstract)
void someMethod();
// Default method
default void someDefaultMethod() {
// Implementation
}
// Static method
static void someStaticMethod() {
// Implementation
}
// Private method
private void somePrivateMethod() {
// Implementation
}
}Resolving Default Method Conflicts
Default method conflicts arise when a class implements multiple interfaces that define default methods with the same signature. The rules to resolve such conflicts are straightforward:
- If a superclass provides a concrete method with the same signature, it takes precedence over any interface default method.
- If multiple interfaces provide default methods with the same signature, the class must override the method to resolve the conflict explicitly.
Get Guhan B’s stories in your inbox
Join Medium for free to get updates from this writer.
Relationship Between Class & Interface
Just like a class can extend another class, it can also implement one or more interfaces. However, a class can extend only one other class but can implement multiple interfaces. A class can implement an interface using the implements keyword.
class ClassName implements InterfaceA, InterfaceB {
// ...
}An interface can also extend one or more other interfaces.
interface InterfaceName extends InterfaceA, InterfaceB {
// ...
}Types of Interfaces
Apart from the Regular Interface discussed above there are two other specialized interfaces types in Java.
Functional Interface
A Functional Interface is an Java interface that contains only a Single Abstract Method (Hence, also known as SAM Interface). Since functional interfaces have only one abstract method they server as the target type for Lambda Functions and Method References. Examples built-in functional interfaces are Comparator, ActionListener, Runnable & Callable.
@FunctionalInterface
interface Consumer<T> {
void accept(T value);
}Functional interfaces have be discussed in details in this article — Java Functional Interface.
Marker Interface
A Marker Interface is an interface with no methods or fields i.e. It is an empty interface. It acts as a tag to convey metadata to the JVM or frameworks. Examples of built-in marker interfaces are Serializable, Cloneable & Remote.
interface Cloneable { }Conclusion
In this article, we explored Interfaces in Java — blueprints that specify a set of requirements a class must fulfill. We discussed their key properties, how they relate to classes, and the different types of interfaces in Java, such as functional and marker interfaces.









