Milo is a high school senior who is very involved politically and is currently volunteering for Elizabeth Warren's campaign. He is an active leader in a variety of extracurricular activities both at his school and in his community. Milo enjoys swimming, performing community service, learning about computer science, studying history, and reading news articles.

What is an interface❓

An interface is like an abstract class, but a class can inherit multiple interfaces while only one abstract class can be inherited. Like abstract classes, you use interfaces when there are several classes that will have similar methods available. Even though you don’t declare them abstract, all methods in an interface are abstract.

Interfaces cannot be instantiated. You can’t do “Interface a = new Interface();”. 🚫

In an interface, you define methods and variables that the child classes will inherit.


Inside an interface, all methods are abstract methods. 

  • An abstract method must be left with no body (no braces, just put a semicolon after the method header)
  • An abstract method is basically a promise that the method will exist and be implemented in a child class
  • Any regular class derived from your interface has to implement the abstract methods (you have to give the method a body)

Example: 🤨

public interface EatInterface
{
   public void hungry(int x);
}

public interface DrinkInterface
{
   public String thirsty(boolean isDehydrated);
}

As you can see,  interfaces are usually just filled with method headers that end with a semicolon.

Why use an interface in this situation? 🤷‍♂️
There are certain types of food that you can both eat and drink (like ramen). By using interfaces, you can inherit functions related to eating from one interface and other functions related to drinking from another interface.

Important Points: 👀

  • If you implement interfaces, the code will not compile until you give a body to the abstract methods (this is the same as an abstract class)
public class Ramen implements EatInterface, DrinkInterface
{
   public void hungry(int x)
   {
      System.out.println("Keep Eating");
   }

   public String thirsty(boolean isDehydrated)
   {
      return "More water";
   }

   // There may be instance variables, constructors, and other methods not
   // shown.
}
  • Note that the class “implements” the interface 
  • A class can extend a parent class and implement interfaces; if there was a class called Noodle, you could do:
public class Ramen extends Noodle, implements EatInterface, DrinkInterface
{
   // There may be instance variables, constructors, and other methods not
   // shown.
}
  • A class can inherit many interfaces (this is different from abstract classes)
  • All methods in an interface are abstract (this is different from abstract classes)
  • Variables (also called fields) in an interface are static and final variables (these variables belong to the class and cannot be changed)
  • Interfaces can be used to contain constants (like Physics constants)

Summary: 🎉✨😎

  • An interface is like an abstract class except all methods are abstract
  • An interface cannot be instantiated
  • All variables in an interface are static and final
  • A class that implements interfaces must give a body to all of the abstract methods
  • A class can inherit multiple interfaces
  • For methods in an interface, just put a semicolon after the method header (no braces)