Section 7.4 Interface Syntax: A Compiler-Enforced Contract
An interface in Java is like a
contract that says, "Any class that implements me promises to contain certain methods." Inside an interface, you only write
method signatures—no actual code bodies. Java then ensures at compile time that implementing classes match those signatures exactly.
Below is the
basic syntax for declaring an interface, broken down step by step:
/*
* 1) 'interface' keyword declares that this is an interface (not a class).
* 2) InterfaceName should be capitalized, like a class.
* 3) Each method signature is implicitly 'public abstract'.
*/
public interface InterfaceName {
/*
* A method signature, with:
* - return type (e.g., void, int, etc.)
* - method name (e.g., play, computeArea, etc.)
* - any parameters (e.g., none or (String message), etc.)
*/
returnType methodName(parameterList);
// You can define multiple signatures if needed
returnType anotherMethod(...);
}
-
public ensures the method is accessible to any class that wants to implement the interface (hence you can’t have a private or package-private method signature fulfilling a public interface requirement—this leads to a compiler error).
-
abstract means there’s no method body—just a promise that classes will provide the details.
Important: If a class tries to implement an interface method but
omits public (for instance, using package-private or private), Java will complain that it doesn’t match the interface’s public method signature.
For our media example, let’s create a simple
Playable interface with one method,
play():
/**
* Represents any media content that can be "played."
*
* Classes implementing Playable promise to provide
* a 'play()' method, ensuring a consistent way to
* start the media.
*/
public interface Playable {
/**
* Starts playing this media content from the beginning.
*/
void play(); // no method body allowed here
}
Tip: Start with a minimal interface (maybe just
void play()) and later add more methods if needed. This matches the Design Recipe’s "incremental" approach.
Below is a more advanced snippet showing how JavaDoc might look with
@param,
@return, and
@throws tags—and
why they matter:
/**
* AdvancedMedia is anything that can be played from
* a specific start position.
*
* In IDEs like VS Code or IntelliJ, these comments
* appear as tooltips when you hover over 'playFrom'.
* Also, 'javadoc' commands can generate an HTML site
* from these tags automatically.
*/
public interface AdvancedMedia {
/**
* Start playing from a specific position (in seconds).
*
* @param startPosition the playback position in seconds,
* must be non-negative
* @throws IllegalArgumentException if startPosition is negative
*/
void playFrom(int startPosition);
}
@param explains each parameter,
@throws indicates when an exception is thrown, and
@return (not shown here) describes what a method returns.
These tags help others (and you!) quickly see how to call or implement the method.
Compiler Error Mini Example
// Suppose our interface is:
public interface ExampleInterface {
void doSomething();
void doAnotherThing();
}
// Class that implements only one method, forgetting doAnotherThing():
public class PartialImplementation implements ExampleInterface {
@Override
public void doSomething() {
System.out.println("Did something!");
}
// 'doAnotherThing()' is missing!
}
Line by line, the compiler is saying: "
doAnotherThing() was promised but not provided." This immediate feedback prevents silent runtime bugs.
If an interface has multiple methods, you must implement them all.
Checkpoint 7.4.1. Interface Purpose.
What is the primary purpose of an interface in Java?
To allow multiple inheritance in Java.
-
- To create objects directly.
-
To define a contract that multiple classes can implement.
Interfaces define a set of abstract methods that implementing classes must provide, enabling a common structure without enforcing a specific implementation.
To improve performance by reducing memory usage.
-
Checkpoint 7.4.2.
Given the following interface and class:
interface Animal {
void makeSound();
}
class Dog implements Animal {
private String type;
public Dog(String type){
this.type = type;
}
}
Why does this code produce a compilation error?
The Dog class cannot implement an interface.
Any Java class can implement an interface in Java.
The Dog class must implement the makeSound() method.
Since Animal is an interface with an abstract method, any class implementing it must provide an implementation for makeSound().
The Dog class needs to explicitly declare the Animal interface methods as public.
While interface methods are implicitly public, the issue here is not visibility as there is no implementation.
Interfaces must have at least one concrete method.
Interfaces do not require concrete methods.
You have attempted
of
activities on this page.