OCA Java SE 8 Class Design - Java Interface








Implementing Interfaces

Java does allow classes to implement any number of interfaces.

An interface is an abstract data type that defines a list of abstract public methods that a class must implement.

An interface can also include a list of constant variables and default methods.

In Java, an interface is defined with the interface keyword.

A class invokes the interface by using the implements keyword in its class definition.

An interface is not declared an abstract class, although it has many of the same properties of abstract class.

The method modifiers are assumed abstract and public.

Whether or not you provide them, the compiler will automatically insert them as part of the method definition.

A class may implement multiple interfaces, each separated by a comma.

public class Rectangle implements A, B, C{ 
} 

In the example, if any of the interfaces defined abstract methods, the concrete class Rectangle would be required to implement those methods.





Defining an Interface

The following is a list of rules for creating an interface.

Interfaces cannot be instantiated directly.

An interface can no methods.

An interface cannot be marked as final.

Marking an interface as private, protected, or final will trigger a compiler error.

Marking a method as private, protected, or final will trigger compiler errors as these are incompatible with the abstract and public keywords.

public interface Printable {} 

It compiles without issue, since interfaces are not required to define any methods. Now consider the following two examples, which do not compile:

public class Main { 
  public static void main(String[] args) { 
    Printable example = new Printable();  // DOES NOT COMPILE 
  } 
} 

public final interface Printable {  // DOES NOT COMPILE 
} 

Printable is an interface and cannot be instantiated directly.

The second Printable doesn't compile since interfaces may not be marked as final for the same reason that abstract classes cannot be marked as final.





Example

For example, the following two interface definitions are equivalent, as the compiler will convert them both to the second example:

public interface Printable { 
  void myMethod(int speed); 
  abstract void fillPaper(); 
  public abstract double draw(); 
} 

public abstract interface Printable { 
  public abstract void myMethod(int speed); 
  public abstract void fillPaper(); 
  public abstract double draw(); 
} 

The abstract keyword is first automatically added to the interface definition.

Each method is prepended with abstract and public keywords.

If the method already has either of these keywords, then no change is required.

Let's take a look at an example that violates the assumed keywords:

private final interface Printable {  // DOES NOT COMPILE 
  private void fillPaper(int depth);  // DOES NOT COMPILE 
  protected abstract double depth();  // DOES NOT COMPILE 
  public final void draw();  // DOES NOT COMPILE 
} 

The first line is marked as final, which cannot be applied to an interface since it conflicts with the assumed abstract keyword. Then, it is marked as private, which conflicts with the public or default required access for interfaces.

The second and third line cannot be marked as private or protected because all interface methods are assumed to be public.

The last line cannot be marked as final since interface methods are assumed to be abstract, the compiler throws an exception for using both abstract and final keywords on a method.

Inheriting an Interface

An interface that extends another interface.

An abstract class can implement an interface and inherit all of the abstract methods as its own abstract methods.

The first concrete class that implements an interface must provide an implementation for all of the inherited abstract methods.

An interface may be extended using the extend keyword.

The new child interface inherits all the abstract methods of the parent interface.

An interface may extend multiple interfaces.

Consider the following example:

public interface Printable { 
  public int getTailLength(); 
} 

public interface Formattable { 
  public int getPageNumber(); 
} 

public interface Paper extends Printable, Formattable { 
} 

Any class that implements the Paper interface must provide an implementation for all methods in the parent interfaces-in this case, getTailLength() and getPageNumber().

Example 2

When an abstract class implements an interface, the abstract class inherits the abstract methods of the interface but is not required to implement them.

The first concrete class to extend the abstract class must implement all the inherited abstract methods of the interface. We illustrate this in the following example:

public interface Printable { 
  public int getTailLength(); 
} 

public interface Formattable { 
  public int getPageNumber(); 
} 

public abstract class AbstractPaper implements Printable, Formattable { 
} 

public class MyPaper implements Printable, Formattable {  // DOES NOT COMPILE 
} 

AbstractPaper is an abstract class and compiles without issue.

Any class that extends AbstractPaper will be required to implement all of the methods in the Printable and Formattable interface.

MyPaper is not an abstract class, and it must implement all the interface methods within its definition.