01 / 21

Defining & Implementing
Interfaces

Learn how Java interfaces create contracts that classes must follow: defining abstract behaviors and building flexible, reusable code architectures.

Java

What Is an Interface?

An interface in Java is a reference type that defines a contract: a set of method signatures that any implementing class must provide. Think of it as a blueprint that says what a class can do, without saying how it does it.

Why Use Interfaces?

Abstraction
Hide implementation details and expose only what matters to callers.
Polymorphism
Treat different classes identically if they share the same interface type.
Multiple Types
A class can implement many interfaces, unlike single-class inheritance.

Anatomy of an Interface

interface Drawable access modifier public keyword interface name Drawable void draw(); abstract method double getArea(); abstract method Methods are public & abstract by default

The Syntax Rule

// Defining an interface public interface Flyable { void takeOff(); void land(); double getAltitude(); }
// Implementing an interface public class Eagle implements Flyable { public void takeOff() { /* body */ } public void land() { /* body */ } public double getAltitude() { return 500.0; } }

What Can an Interface Contain?

Abstract Methods
No body. Implicitly public abstract. The core of every interface.
Default Methods
Have a body using the default keyword. Provide optional behavior.
Constants
Fields are implicitly public static final. Always constant values.

Interface vs Abstract Class

Interface Abstract Class
Methods Abstract + default Abstract + concrete
Fields Constants only Any fields
Constructors ✕ No ✓ Yes
Multiple ✓ Many interfaces ✕ One class only
Keyword implements extends

How It Fits Together

<<interface>> Drawable Dog draw() { ... } getArea() { ... } Circle draw() { ... } getArea() { ... } Car draw() { ... } getArea() { ... } Each class provides its own implementation implements = dashed arrow in UML

How to Apply It

1
Identify shared behavior. What actions do multiple unrelated classes need to perform?
2
Declare the interface. Write public interface Name and list abstract method signatures.
3
Implement in each class. Add implements Name to the class header.
4
Override every method. Provide a public body for each abstract method.
5
Use the interface type. Declare variables as the interface type for polymorphism: Drawable d = new Circle();

Example 1: Simple Interface

public interface Speakable { String speak(); }
public class Dog implements Speakable { public String speak() { return "Woof!"; } } public class Cat implements Speakable { public String speak() { return "Meow!"; } }

Example 2: Multiple Interfaces

public interface Swimmable { void swim(); } public interface Flyable { void fly(); }
public class Duck implements Swimmable, Flyable { public void swim() { System.out.println("Paddling..."); } public void fly() { System.out.println("Flapping wings..."); } }

Example 3: Polymorphism in Action

public interface Shape { double getArea(); String getName(); }
Shape[] shapes = {new Circle(5), new Rectangle(3, 4)}; for (Shape s : shapes) { System.out.println(s.getName() + ": " + s.getArea()); } // Output: // Circle: 78.54 // Rectangle: 12.0

Default Methods in Interfaces

public interface Logger { void log(String message); default void logError(String msg) { log("ERROR: " + msg); } }

A default method has a body inside the interface itself. Implementing classes inherit this method automatically but can override it if needed.

Interface Inheritance

public interface Movable { void move(); } public interface Turbo extends Movable { void boost(); }

Interfaces can extend other interfaces using the extends keyword. A class implementing Turbo must provide bodies for both move() and boost().

Common Mistakes

✕ Forgetting public
Interface methods are public by default. The implementing method must also be public, or the compiler reports a weaker access error.
✕ Missing methods
If you implement an interface but skip one method, the class won't compile unless you declare it abstract.
✕ Instantiating an interface
You cannot write new Drawable(). Interfaces have no constructor: use a class that implements it.

Summary Table

Concept Details
Keyword interface to define, implements to use
Methods Abstract (no body) + default (with body)
Fields public static final constants only
Multiple A class can implement many interfaces
Inheritance Interfaces extend other interfaces
Instantiation Cannot create an interface object directly
Use case Shared behavior across unrelated classes

Question 1 of 5

Which keyword does a class use to adopt an interface in Java?

Question 2 of 5

What happens if a class implements an interface but does not provide a body for one of its abstract methods?

Question 3 of 5

What is wrong with the Report class?

Question 4 of 5

How many interfaces can a single Java class implement?

Question 5 of 5

Which statement about the Truck class is correct?