Java Tutorial
Java Interfaces
An interface names a contract: method signatures a class must provide. A class implements the interface and supplies the bodies — and one class can implement several interfaces.
A contract without a full class
An interface lists what something can do, not how. Classes that implements it must provide those methods. Variables can use the interface type so callers depend on the contract, not a concrete class.
Example
interface Speaker {
void speak();
}
class Dog implements Speaker {
public void speak() {
System.out.println("woof");
}
}
public class Main {
public static void main(String[] args) {
Speaker s = new Dog();
s.speak();
}
}Multiple implementations
Any number of classes can implement the same interface. Code written against Speaker works with all of them — the same polymorphism idea you saw with inheritance, without forcing a shared superclass.
Example
interface Speaker {
void speak();
}
class Dog implements Speaker {
public void speak() { System.out.println("woof"); }
}
class Robot implements Speaker {
public void speak() { System.out.println("beep"); }
}
public class Main {
public static void main(String[] args) {
Speaker[] all = { new Dog(), new Robot() };
for (Speaker s : all) s.speak();
}
}One class, many interfaces
A class may implement several interfaces, comma-separated. That is how Java models multiple capabilities when single inheritance is not enough: printable and comparable, closable and readable.
Example
interface Named {
String name();
}
interface Priced {
double price();
}
class Product implements Named, Priced {
private String name;
private double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
public String name() { return name; }
public double price() { return price; }
}
public class Main {
public static void main(String[] args) {
Product p = new Product("Mug", 9.5);
System.out.println(p.name() + " " + p.price());
}
}Interface methods are public when implemented — the compiler requires it. Since Java 8, interfaces may also contain default methods with a body; start with abstract method lists first.
Try It Yourself
Exercise: Define interface Greeter { void greet(String name); }. Implement it in a FriendlyGreeter that prints "Hi, " + name. Call it through aGreeter variable.
Show solution
interface Greeter {
void greet(String name);
}
class FriendlyGreeter implements Greeter {
public void greet(String name) {
System.out.println("Hi, " + name);
}
}
public class Main {
public static void main(String[] args) {
Greeter g = new FriendlyGreeter();
g.greet("Ada");
}
}The variable type is the interface; the object supplies the implementation.
Key Takeaways
- An interface declares a contract; classes
implementsit and supply the methods. - Interface-typed variables enable polymorphism without a shared superclass.
- One class can implement multiple interfaces.
- Prefer interfaces for capability contracts; prefer abstract classes when sharing real code.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Biology
A measurable contract
An interface names a capability. Anything that implements Measurable must supply a reading, whether it is a leaf or a sensor.
Example
interface Measurable {
double reading();
}
class Probe implements Measurable {
public double reading() { return 37.0; }
}
public class Main {
public static void main(String[] args) {
Measurable m = new Probe();
System.out.println(m.reading());
}
}