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 implements it 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());
  }
}

FAQ: Java Interfaces

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java interfaces examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java interfaces in this Java Java lesson (Java Interfaces).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.