Java Interfaces
Declare an interface with empty method headers, then fill those methods in a class with implements. Write interface Signal with void horn(); and no body. Write class Truck implements Signal and give horn a body that prints honk. In Yard.java, build a Truck and call t.horn(). Compile with javac Yard.java and run java Yard. The screen should show honk. A second demo lets one class implements two interfaces at once. Leave enums for the next page — this lesson is the promise of a method and the class that keeps it.
Implement one interface method
The overriding page rewrote horn on a child that already extends Car. This page starts from a promise with no body. Open Yard.java. Keep the public class name Yard. Delete leftover Car/Truck extends demos if they bury the interface story, leftover constructor super prints, leftover path demos, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Declare the interface above the classes:
interface Signal {
void horn();
}
The header ends with a semicolon. There is no { } body for horn inside the interface on this beginner shape.
Declare the class that keeps the promise:
class Truck implements Signal {
Inside Truck, write a public method with the same name and fill the body:
public void horn() {
System.out.println("honk");
}
The keyword is implements, not extends. The class must provide a body for every method listed on the interface. On this page that is only horn.
Inside Yard.main, build a truck and call the method:
Truck t = new Truck();
t.horn();
The call runs the body written on Truck, so the screen prints honk.
interface Signal {
void horn();
}
class Truck implements Signal {
public void horn() {
System.out.println("honk");
}
}
public class Yard {
public static void main(String[] args) {
Truck t = new Truck();
t.horn();
}
}
From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard. The interface Signal and the helper class Truck stay without public on this beginner shape.
The screen shows honk. Walk the run once. Java starts at Yard.main. new Truck() builds a truck that promised Signal. t.horn() runs the body on Truck and prints honk. You never write new Signal() — the interface names the methods; the class supplies the bodies and the object.
If javac refuses, check three things. First, the class line must say implements Signal, not extends Signal. Second, every interface method needs a matching method on the class. Third, the implement method is public on this teaching shape so it matches the interface’s public contract. If you still see beep from an old override file, switch to the interface teaching file and run java Yard again.
Say the rule out loud while you watch the screen: interface names the call, class fills the body, object runs that body. Keep implements on the class that owns the print. Do not invent an enum on this page. Enums wait for the next lesson.
Implement two interfaces on one class
A second program keeps Yard and Truck. Now Truck implements two interfaces. Signal still asks for horn. Mark asks for show. The class fills both bodies. main calls both methods.
interface Signal {
void horn();
}
interface Mark {
void show();
}
class Truck implements Signal, Mark {
public void horn() {
System.out.println("honk");
}
public void show() {
System.out.println("truck");
}
}
public class Yard {
public static void main(String[] args) {
Truck t = new Truck();
t.horn();
t.show();
}
}
Compile with javac Yard.java, then run java Yard. The screen shows honk, then truck. The first line comes from the Signal promise filled on Truck. The second line comes from the Mark promise filled on the same class. Separate interface names with a comma in the implements list: implements Signal, Mark.
Read the class header once more. One class. Two interfaces. Two method bodies. That is the beginner picture of keeping more than one promise without using two extends parents. Java lets a class extend only one class; it can implement several interfaces. This page shows that comma list in a small Yard file.
If you omit show while Mark is listed, javac refuses because every listed interface method needs a body. If you omit Mark from the implements list but still write show, you have an ordinary method, not an interface fulfillment for Mark. Keep both names on the class line when you want both promises checked.
Compared with extends override
On the overriding page, Truck extends Car and rewrote horn that already had a parent body. On this page, Signal starts with no body, and Truck implements Signal supplies the first body. You still compile Yard.java and run java Yard. You still call t.horn(). The new tokens are interface, implements, and — in the second demo — a comma-separated implements list.
If a leftover override file is still open, you will see extends Car and maybe beep beside honk. Switch to the interface teaching file so the top of the file shows interface Signal and the class line shows implements. Keep enum demos off this page. Keep keyboard input off this page.
What this leaves for enums
This page fills interface methods with implements. The next page shows enums, where a type lists a fixed set of named values. Stay with interface promises here. Do not replace today’s demos with an enum list as a requirement yet.
When you later use enums, you may still implement interfaces on other types in the same program. Treat this page as the interface half of that neighboring story. Finish both Yard demos until honk — and then honk plus truck — feel automatic before you open the enums lesson.
Before enums
You can write interface Signal with empty horn(), implement it on Truck, and see honk from t.horn(). You can implement Signal and Mark on one class and see honk then truck. Compile Yard.java and run java Yard for each kept demo in turn. The class kept the interface promises. The next page is enums, where a type lists fixed names.
Keep the single-interface honk demo for a clear implements story. Keep the two-interface demo for a clear comma list. Keep enum files off this page. Those three habits are the checkpoint before you move on. When honk appears after t.horn() — and when the second demo also prints truck after show() — interfaces did their job for this lesson and you are ready for the next topic about enums.
One last pass through the folder: one public Yard, interface headers with empty methods, a class that implements them, no keyboard wait, no forced enum. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still prints the interface fills you wrote, the promises are solid and the course can move to enums on the next page.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top