Interfaces (intro & contrasts)
Define interfaces, implement them in classes, and contrast with abstract classes. Learn default methods and multiple implementation.
Interfaces (intro & contrasts) is a free Java Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.
What is an interface?
Interface: a contract that lists method signatures a class must provide. Use it to describe capability, like Drivable or Printable, without fixing class hierarchies.
Code: implement an interface
Classes implement interfaces to promise certain behavior. You can reference the object by its interface type and call the declared methods.
public class Main {
// Interface that declares a capability
static interface Drivable {
void drive(); // method to implement
}
// A class implements the interface
static class Car implements Drivable {
public void drive() {
System.out.println("Car is driving");
}
}
public static void main(String[] args) {
Drivable d = new Car(); // upcast to interface
d.drive(); // runs Car's method
}
}
Interfaces vs abstract classes
Abstract class = is-a with shared code. Interface = can-do capability. A class can extend one class but implement many interfaces, which keeps designs flexible and avoids deep trees.
Code: default method
Interfaces can have default methods with code. Classes that implement the interface inherit that small behavior automatically.
public class Main {
// Interface with a default method (has a body)
static interface Greeter {
void greet(String name);
default void sayBye() { // default provides a reusable tiny implementation
System.out.println("Bye");
}
}
static class SimpleGreeter implements Greeter {
public void greet(String name) {
System.out.println("Hello, " + name);
}
}
public static void main(String[] args) {
Greeter g = new SimpleGreeter();
g.greet("Ada");
g.sayBye(); // default method from the interface
}
}
Multiple interfaces
A class may implement several interfaces at once (e.g., Drivable and Maintainable). This models capabilities without forcing a fixed superclass chain.
Code: multiple interfaces
One class implements both interfaces. You can treat it as Drivable, Maintainable, or its concrete type depending on the context.
public class Main {
static interface Drivable { void drive(); }
static interface Maintainable { void maintain(); }
static class ServiceCar implements Drivable, Maintainable {
public void drive() { System.out.println("ServiceCar driving"); }
public void maintain() { System.out.println("ServiceCar maintenance"); }
}
public static void main(String[] args) {
ServiceCar s = new ServiceCar();
s.drive();
s.maintain();
}
}
Interfaces check
Quick check: Which statement about interfaces in Java is correct?
Recap
Recap: Interfaces define behavior contracts; classes implement them. Use defaults for tiny shared code and combine multiple capabilities in one class when needed.
Learn Java with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 104
- Lessons
- 374
Frequently Asked Questions
Is the “Interfaces (intro & contrasts)” lesson free?
Yes — the full text of “Interfaces (intro & contrasts)” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.
What will I learn in “Interfaces (intro & contrasts)”?
Define interfaces, implement them in classes, and contrast with abstract classes. Learn default methods and multiple implementation. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
How long does the “Interfaces (intro & contrasts)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Abstract Classes
- Interfaces (intro & contrasts)
- Up/Down Casting & instanceof