0Pricing
Java Academy · Lesson

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 — 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

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, and the Java Academy course includes 3 lessons in total. 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.

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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

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

  1. Abstract Classes
  2. Interfaces (intro & contrasts)
  3. Up/Down Casting & instanceof
← Back to Java Academy