E ExamMaster

Object-Oriented Programming through Java · Arrays and Inheritance

Interfaces and Annotations

In Object-Oriented Programming through Java because a ticket can promise Printable and Refundable at once — interfaces, default and static methods, functional types and…

An interface is a promise of methods, not a mould with fields the way a class is. Ticket can implement two interfaces. Interfaces may nest, offer default bodies, and hold static helpers. A functional interface has one abstract method. An annotation is a label the compiler or a tool can read.

  • Object-Oriented Programming through Java
  • Easy level
  • 7 concepts

1An interface names a promise

An interface declaration lists methods a class agrees to have. interface Printable { void printStub(); } does not make an object. It names a contract.

A class writes implements Printable and then supplies the method. The desk can type a variable as Printable and not care whether the object is a Ticket or a receipt.

Figure. Printable names printStub(); it does not make an object. Ticket implements the contract. A variable typed Printable can hold that Ticket.

How it works

  1. An interface names a promiseAn interface is a contract. implements means the class will write the methods.

Ticket keeps the Printable promise

public interface Printable {
    void printStub();
}
public class Ticket implements Printable {
    public void printStub() {
        System.out.println(seat);
    }
}

2A class may implement several interfaces

Java forbids two parent classes and allows many interfaces. Ticket can be Printable and Refundable at once.

That is how the desk shares behaviour across types that are not in one inheritance line.

Figure. One class, two contracts. This is not two parent classes.

How it works

  1. A class may implement several interfacesMany interfaces, one class. That is the legal form of multiple inheritance in Java.

Two interfaces, one class

public class Ticket
        implements Printable, Refundable {
}

3An interface may be nested

A nested interface is declared inside a class or another interface. Ticket.Code can name the barcode contract without a top-level file.

It is still a contract. Nesting is packaging, not a new kind of inheritance.

Figure. Code sits inside Ticket as Ticket.Code. Nesting is a folder for the barcode contract, not a second inheritance line.

How it works

  1. An interface may be nestedNest an interface when the contract only belongs beside that type.

Code is nested in Ticket

public class Ticket {
    public interface Code {
        String barcode();
    }
}

4default gives the interface a body

A default method is an interface method with a body. Old classes that implement Printable pick up printTwice() without being edited.

A class may still override the default. If two interfaces default the same signature, the class must override and choose.

Figure. printTwice has a body on Printable. Ticket already implements Printable, so it receives that body without being edited. Two defaults with the same name still force an override.

How it works

  1. default gives the interface a bodydefault is a body on the interface. Two defaults with the same name force the class to override.

printTwice has a body on the interface

public interface Printable {
    void printStub();
    default void printTwice() {
        printStub();
        printStub();
    }
}

5static on an interface is a helper

A static method on an interface is called on the interface name: Printable.banner(). It is not inherited as an instance method and is not overridden.

Use it for a small helper that belongs with the contract, not with one implementing class.

Figure. banner is static on Printable, so the call is Printable.banner(). It is not inherited as an instance method and is not a dispatch target.

How it works

  1. static on an interface is a helperInterface static methods are called on the interface. They are not dispatch targets.

Call Printable.banner()

public interface Printable {
    static String banner() {
        return "CINEMA";
    }
}

6One abstract method may be a lambda

A functional interface has exactly one abstract method. @FunctionalInterface asks the compiler to keep that true.

The desk can then write the method as a lambda: RefundRule student = price -> price / 2; instead of a named class that implements one method.

Figure. RefundRule has one abstract method, apply. That is enough for a lambda: price -> price / 2. @FunctionalInterface asks the compiler to keep the count at one.

How it works

  1. One abstract method may be a lambdaOne abstract method makes a functional interface. A lambda is a short object that keeps that promise.

One abstract method, then a lambda

@FunctionalInterface
public interface RefundRule {
    int apply(int price);
}
RefundRule student = price -> price / 2;

7An annotation is a label a tool can read

An annotation is written with @ and is itself a special interface. @Override, @Deprecated and @FunctionalInterface are the ones this course already used.

You can declare your own: @interface DeskOnly { }. The compiler, a test runner, or a framework reads it. It is not a runtime if-statement unless a tool looks it up.

Figure. @Override is a label on printStub. The compiler reads it and fails if the signature does not match the parent. It is not a runtime if unless a tool looks it up.

How it works

  1. An annotation is a label a tool can readAnnotations label code for tools. @Override is the one that catches a mistyped override.

@Override is an annotation the compiler checks

@Override
public void printStub() {
    System.out.println(seat);
}

Notes

  • In Object-Oriented Programming through Java because a ticket can promise Printable and Refundable at once — interfaces, default and static methods, functional types and annotations.
  • implements lists interfaces. A class may implement many. default methods have a body; static methods are called on the interface name.
  • A functional interface has one abstract method and may be written as a lambda. Annotations such as @Override are interfaces for the compiler.

Exam traps & shortcuts

  • If two interfaces default the same method, the class must override it and pick (or combine) a body.
  • @Override failing to compile is a gift: the signature did not match the parent.

Recap

An interface is a contract. A class may keep many. default and static live on the interface. One abstract method is functional. @ labels code.

implements, many times
Two interfaces are legal. Two parent classes are not.
default vs static
default is an instance body. static is Printable.banner().
functional
One abstract method. A lambda may implement it.
@Override
An annotation. If it fails, the signature did not match.

Practise Interfaces and Annotations

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.