ByteBrawl - Example Interface - Interfaces - Inheritance

Interfaces - Example Interface

An interface is a contract that defines a set of fields, methods, and nested classes that a class must implement. In order for a class to use an interface, the implements keyword must be used. A class can implement multiple interfaces.

ExampleInterface.java

Main.java

In this example, MyClass implements the ExampleInterface interface, which requires it to provide an implementation for the display method. When we create an instance of MyClass and call display, it executes the method defined in MyClass. However, notice how we had to put public before void display in MyClass. This is because interface methods are implicitly public abstract, so the implementation must also be public.