top of page
  • Writer's pictureMeghan Gill

Interfaces in Java

What is an interface?

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.

Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

What does an interface look like?

Access Modifiers

Interfaces need specific access modifiers. Specify that the interface is public, otherwise your interface is accessible only to classes defined in the same package as the interface.

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

All constant values defined in an interface are implicitly public, static, and final. You can omit these modifiers.


An Interface's Body

The interface body can contain abstract methods, default methods, static methods and constants.

The method signatures have no braces and are terminated with a semicolon. Method bodies exist only for default methods and static methods.

  • An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).

  • Default methods are defined with the default modifier.

  • Static methods with the static keyword.


How are interfaces used?

To use an interface, you write a class that implements the interface. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. You must override the method with your own implementation.

An interface can extend other interfaces, just as a class can extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces.

63 views0 comments
bottom of page