Declaring Interfaces

An interface is a reference type similar to an abstract class but it can contain only:

  • Constants. All fields defined in an interface are implicitly public, static, and final (these modifiers can and should be omitted)
  • Abstract methods to be implemented by classes implementing the interface. Except for default methods, all methods declared in an interface are implicitly public and abstract (both can be omitted). They can’t be static.
  • Default methods. Declared with the default modifier. They are methods with implementation.
  • Nested types (you can learn more about them in the lesson about nested classes)

The following pseudocode summarizes the basic syntax for an interface declaration:

Interfaces are implicitly abstract and they:

  • Can be only public or default (no access modifier), because they have to be visible to be implemented)
  • Can’t be final (they always have to be extendable)
  • Can extend more than one interface (unlike classes that can only extend one superclass)

The following is a basic interface example, including a default method:

You must also know that empty interfaces are valid (and are usually used as object tags):

More About Default Methods

Default methods allow adding new methods to an existing interface without breaking backwards compatibility. Pre-existing classes implementing that interface are free to use or not the new method, but they are not required to be modified in order to keep working.

If you extend an interface that contains default methods, the choices for each of them are:

  • Override it, the same way a class would override a superclass method
  • Inherit it by not mentioning it at all in your extended interface
  • Re-declare it, which makes it abstract

Now that you know how to declare interfaces, let’s see how to use them.

Using Interfaces


Accessing Interface Constants

Interface constants are inherited by implementing classes. These constants (static fields) can be accessed in different ways. The code example below illustrates this concept:

Interface References

We could say an object has multiple types when it implements one or more interfaces: its own class and all those interfaces. Therefore an interface reference can be used anywhere a type reference can be used: variables, fields, parameters, return values, etc. The referenced value can be any object of a class that implements the interface and you can use type casting to tell the compiler what the object really is:

Remember: if you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements that interface.

Interface Type Is Inherited

If a class B extends from a class A implementing the interface I, then B is also an I:

You can also use a redundant declaration in the subclass and it’s still valid (and you don’t have to implement the interface methods again in the subclass):

Finally, in case you don’t know, multiple casting is allowed by Java so the following code is also valid:

In this example:

  • The first casting –(I) b– is OK because b is always an (since B implements I)
  • The second casting –(B)((I) b)– is also OK because ((I) b) is an I reference and so it could be a B at runtime (and it is in this case)

You probably will find code like this in the exam and now that you know multiple casting is allowed it won’t catch you off guard!


Leave a Reply