Skip to main content
A Java interface is an abstract reference type used to specify a strict contract of behavior that implementing classes must fulfill. It acts as a structural blueprint containing method signatures, default methods, static methods, and constant declarations, without dictating instance-level state or memory allocation for objects.

Syntax and Member Types

Interfaces are declared using the interface keyword. Depending on the Java version, an interface can contain several specific types of members:

Implementation Mechanics

A concrete class binds to an interface using the implements keyword. The class is strictly required to provide concrete implementations for all abstract methods declared in the interface, unless the class itself is declared abstract.

Technical Constraints and Rules

  • Instantiation: Interfaces cannot be instantiated directly using the new keyword. They must be implemented by a class or instantiated via an anonymous inner class.
  • State: Interfaces cannot maintain instance state. They cannot declare instance variables; any field declared is implicitly a compile-time constant (public static final).
  • Constructors: Interfaces do not possess constructors, nor can they participate in the initialization chain of an object’s state.
  • Access Modifiers: Abstract methods, default methods, and constants are implicitly public. Omitting an access modifier does not make the member package-private; it implicitly remains public. Attempting to explicitly assign the protected or private modifier to an abstract method results in a compilation error.

Inheritance and Multiple Types

Unlike classes, which are restricted to single inheritance, interfaces support multiple inheritance of type. Class Implementing Multiple Interfaces: A single class can implement an arbitrary number of interfaces, separated by commas.
Interface Extending Interfaces: An interface can inherit from multiple other interfaces using the extends keyword. This combines the contracts of the parent interfaces into the child interface.

Default Method Conflict Resolution

Because a class can implement multiple interfaces, it may inherit multiple default methods with identical signatures, creating a multiple-inheritance ambiguity (the Diamond Problem). The Java compiler resolves this by forcing the implementing class to override the conflicting method and explicitly specify which interface’s default method to invoke using the super keyword.
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More