Chapter:Interfaces - Rules and syntax

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.


Background about this chapter topics...


Make the student familiar with stuff brought up in this chapter...


After this chapter, the student shall understand:

  • this
  • that
  • those
  • them
  • and the reset

Instructions to the teacher

Common problems

TODO! Students often ask about X and have problems with Y...

Interfaces: Some rules about Java interfaces

General rules

Methods declared in an interface are abstract by default, meaning that even if you don't add the abstract keyword, they will be interpreted as abstract. This means that you have to implement all methods declared in an interface if your class says it implements some interface(s), or else you would have to declare your class as abstract and leave implementation to classes extending your class.

Interfaces differ from abstract classes in that interfaces do not declare constructors, and they cannot deal with state. Instance variables are for this reason something that cannot exist in an interface. Abstract classes work pretty much like ordinary classes with the addition of being able to declare abstract methods. Another important difference from abstract classes, is of course that you cannot extend more than one class (which could be abstract or not), but you can implement many interfaces.

Default methods

In Java version 8, default methods were introduced. In short, a default method in an interface uses the keyword default and allows the author to write a method with a concrete implementation. The main use for this is to allow for methods to be added to an interface without breaking existing implementing classes. Before Java 8, adding a method to an interface would break all implementing classes, since they would lack the implementation for the new method.

Static methods

Since Java 8, interfaces can also have static methods. All members of an interface are implicitly public so you don't have to explicitly declare constants, abstract methods, default methods or static methods as public, but you are of course allowed to (for clarity). If you want a static method in your interface, you declare the method such, using the static keyword.

Remember that static members belong only to the class/interface in which they are declared, so the only way to call a static method from another class, is by the qualified name. Inside the same class or interface, however, you can call a static method by its name, but this is the only place where you can do that.

public interface Storage {

  public abstract void store(Item item);

  public default String status() {
    // call the static method header()
    return header() + " default status message";

  public static String header() {
    return System.getProperty("") + " " + System.getProperty("user.timezone");

The example above shows that a static method can be used as a utility or helper method, called from a default method in an interface. Another typical use of static methods in interfaces are static methods that are specific to an interface (methods that don't make sense outside the interface). Previous to Java 8, such static methods needed to be placed in a class "next to" the interface. Since Java 8, we can put such specific methods inside the interface where they belong (and make sense):

public interface Logger {
  public static final String FILE_SEP = System.getProperty("file.separator");
  public static final String LOG_FILE = FILE_SEP + "tmp" + FILE_SEP + "logfile";

  public abstract void log(String message);

  public static boolean logFileExists() {
    return new;

//Test program:
public class TestLogger {

  // Lazy! Main shouldn't throw exceptions - don't do this at home!
  public static void main(String[] args) throws Exception {


Extending an interface

Extending an interface using inheritance with the keyword extends would work fine but produce a new interface type. This it typically used to add a new subtype which declares a few more methods. A perhaps surprising thing about interfaces and inheritance, is that you can inherit more than one interface (whereas a class only can inherit/extend one single parent class). The problems with multiple inheritance are avoided, since interfaces don't declare concrete methods, and it is not allowed to extend two (or more) interfaces with the same default methods.

If a parent interface declares a constant (variables declared in interfaces implicitly are public static final!), both an extending interface and an implementing class "inherit" the constant, as if it were declared in the class or interface itself.

public interface Interface1 {
  int CONSTANT = 666; // implicitly public static final!

  // Java 8 default method:
  public default void print() {


public interface Interface2 {
  int NUMBER = 7; // implicitly public static final
  public default void print2() {

public interface Interface3 extends Interface1,Interface2 {
  default void printSum() {
    // Interface3 has "inherited" CONSTANT from Interface1 and NUMBER from Interface2
    System.out.println(CONSTANT + NUMBER);

public class TestInterfaces implements Interface3 {
  public static void main(String[] args) {
    TestInterfaces ti = new TestInterfaces();
    ti.print(); // default method indirectly "inherited" from Interface1 
    ti.print2();// default method indirectly "inherited" from Interface2
    System.out.println(NUMBER);  // NUMBER indirectly "inherited" from Interface2
    ti.printSum(); // default method from Interface3
    System.out.println(CONSTANT);// CONSTANT indirectly "inherited" from Interface1

$ javac *.java && java TestInterfaces 

The only thing which can be overridden (in the sense of the word we use when talking about inheritance and classes) would be default methods (which were introduced in Java 8). If you don't override any default methods from the super interface, you will inherit the default implementations from the super interface. If you redefine the default methods as abstract methods, they will "forget" their default implementations from the super interface and become normal abstract methods as any other abstract methods in an interface.

If you are not using Java 8 (or later), you don't have default methods, so you cannot try this without installing Java 8 or later.

One last thing

We thought we'd mention one last thing about interfaces. Since interfaces nowadays can have static methods, the following is actually possible:

public interface IMain {
  public static void main(String[] args) {
    System.out.println("Hello no class");

And thus, you may:

$ javac && java IMain
Hello no class

This of course makes it more challenging for us teachers to talk about interfaces and classes. To say that you need at least one class to create a Java program isn't true anymore, which is obvious from the above. You need at least one class or one interface, in order to create a Java program.

It is, however, still true that not all classes are used as blueprints for objects. A class declaring just the main method still isn't primarily a blueprint for objects, since such a class wasn't designed with the purpose of creating objects from that very class. It was simply designed for declaring the main method.

Chapters on the topic of Interfaces

You can check your progress on this chapter using this list:

You are now done (after the exercises) with the topic of Interfaces! Congratulations, and keep up the good work!

Lecture slides and Videos


Further reading

Where to go next

The next page has exercises (if any) for this chapter: Interfaces - Rules and syntax - Exercises

« PreviousBook TOCNext »