Java Interface Example, Explanation, and Implementation

java interface exampleIf you’re studying interfaces in Java, you most likely already know a thing or two about classes. In fact, you’re probably wondering what the difference between an interface and a class even is, and why you would choose to use an interface instead of a class.

In super simplified terms, an interface is a collection of methods that a class must inherit. The easiest way to think about this is imagining that the class has signed a contract, promising to perform each behavior dictated by the interface.

In this guide, we’ll go what a Java interface is, how to use it, and provide a Java interface example for reference. If this isn’t the tutorial you were looking for, check out this course on the fundamentals of Java, or this introductory Java course for complete beginners for more.

What is a Java Interface?

A Java interface is an abstract type used to designate a set of abstract methods for classes to implement. As mentioned before, when a class implements an interface, it must inherit all of the abstract methods declared within, as though signing into a contract and carrying out the agreement.

The most significant similarity between interfaces and classes is that they both contain methods. They are more different than they are the same, however: an interface lacks constructors, contains exclusively abstract methods (no method implementation), and exclusively final and static fields.

If you don’t know what any of this means, it might be useful to check out this Java course for absolute beginners.

How do You Declare a Java Interface?

Because interfaces are implicitly public and abstract, you do not need to specify either keyword. A very basic example for declaring an interface would look like this:

interface Human
{
   public void breathe();
   public void speak();
}

What Would You Use an Interface For?

Before we move into implementing interfaces, let’s talk a bit about what they’re useful for. Let’s say you’re designing a fantasy game where the player must slay various creatures throughout the game world to survive. All of these enemies share a certain set of attributes, but you don’t want to have to rewrite the same behaviors over and over again for each one. So, you’d declare an interface instead.

For example:

interface Enemy
{
   public void speak();
   public void moveTo(int x, int y);
   public void attack(entity e);
   public void heal(int amt);
   public void eventOnDeath();
}

Any class that implements this interface in your game must speak, move, attack, heal, and have an event after they’re defeated. You might have numerous enemy classes that have their own unique attributes, but each and every one that implements Enemy interface must adhere to the methods listed. Certain enemies might speak differently, attack differently, or do something differently after defeat, but they will all at least do those things.

Learn more about designing and coding games with this Java-based game development course.

How do You Implement an Interface?

An implemented interface on a class of enemies might look something like this:

public class SkeletonNPC implements Enemy
{
   public void speak()
   {
      System.out.println("Skeleton shrieks at the player.");
   }

   public void moveTo()
   {
      System.out.println("Skeleton runs towards the player.");
   } 

   public void attack()
   {
      System.out.println("Skeleton attacks the player.");
   }

   public void heal()
   {
      System.out.println("Skeleton heals itself with a potion.");
   }

   public void throwRock()
   {
      System.out.println("Skeleton throws a rock at the enemy.");
   }

   public void eventOnDeath()
   {
      System.out.println("Skeleton falls apart and drops an item.");
   }
}

For the sake of simplicity, it looks like you’re making a text based adventure! Of course, you can substitute System.out.println for any action. As you can see, the SkeletonNPC class implements all the methods seen in interface Enemy, from speaking, moving, attacking, and more. The SkeletonNPC class also has another behavior, throwRock(), which is unique to that class. It is not included in interface Enemy, because only the SkeletonNPC enemy should have this behavior.

How big the rock that it throws is, or how many it throws, might depend on any child classes the parent SkeletonNPC class might have. For more on this, check out this ultimate Java programming course for beginners.

How to Extend a Java Interface

It’s possible to have an interface extend another interface. Just like classes, the extended child interface will inherit the abstract methods specified in the parent interface.

public interface Vehicles
{
   public void hasWheels();
   public void hasEngine();
}
public interface Car extends Vehicles
{
   public void hasDoors();
   public void hasAirbags();
   public void hasRoof();
}
public interface Motorcycle extends Vehicles
{
   public void hasPedal();
   public void hasHandlebars();
   public void hasStand();
}

In the Java interface example above, we can see the way the Car and Motorcycle interfaces extend the Vehicles interface. The Vehicles interface contains two methods: hasWheels() and hasEngine(). Any class that implements interface Vehicles must meet these two requirements.

If a class implements interface Car, then it must adhere to the three methods laid out in the extension – hasDoors(), hasAirbags(), and hasRoof() – plus the methods indicated in interface Car’s parent interface, Vehicles. Similarly, a class that implements interface Motorcycle must follow the guidelines for both the child and parent interfaces.

A Java interface can extend numerous parent interfaces. It does not adhere to the same multiple inheritance restrictions set on classes in Java, because interfaces have different rules than classes.

To extend an interface, you simply need to use the extends keyword, followed by a list of parent interfaces, separated by commas.

public interface Car extends Vehicles, Belongings, Transit

The above example indicates that the child interface Car is an extension of the Vehicles interface we declared previously, as well as an interface called Belongings, and an interface called Transit. Any methods declared in the Belongings and Transit interfaces will be inherited by the child interface Car.

To learn more about Java interfaces, and more upper-level programming concepts, check out this advanced Java programming course.

If you’re still a beginner, here’s an introduction to coding in Java using the Eclipse IDE.