Introduction to Interfaces
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 34 of 87.
You've seen how abstract classes define contracts that subclasses must follow. But what if you want a class to follow multiple contracts? Java doesn't allow a class to extend multiple classes, but it solves this problem with interfaces.
An interface is a contract that defines what a class can do, without specifying how it does it. Unlike abstract classes, interfaces contain only method signatures (by default) with no implementation:
public interface Drawable {
void draw();
}
public interface Resizable {
void resize(int width, int height);
}Think of an interface as a promise. When a class implements an interface, it promises to provide implementations for all the methods declared in that interface. The interface defines the capability, and the class provides the behavior.
Interfaces are declared using the interface keyword instead of class. All methods in an interface are implicitly public and abstract, so you don't need to write those keywords:
public interface Playable {
void play(); // implicitly public abstract
void pause();
void stop();
}Interfaces can also declare constants, which are implicitly public static final:
public interface GameConstants {
int MAX_PLAYERS = 4; // implicitly public static final
}In the next lesson, you'll learn how classes implement interfaces using the implements keyword.
Challenge
EasyLet's build a device control system by defining interfaces that describe what different devices can do. This challenge focuses on creating interfaces—the contracts that define capabilities without specifying how they work.
You'll organize your code across three files:
Switchable.java: Create an interface that defines what it means for a device to be switchable. Any switchable device should be able to turn on and turn off. Declare two methods:turnOn()andturnOff(). Remember, interface methods are implicitly public and abstract, so you just need the method signatures.Adjustable.java: Create another interface that defines what it means for a device to be adjustable. An adjustable device should be able to set its level. Declare a methodsetLevel(int level). Also include a constantMAX_LEVELwith a value of100—remember that constants in interfaces are implicitlypublic static final.Main.java: Demonstrate that your interfaces are properly defined by printing information about them. You'll receive one input: a level value (int). Print the following three lines:Switchable methods: turnOn, turnOffAdjustable methods: setLevelMax level constant: [MAX_LEVEL value](access the constant from the Adjustable interface)
Setting level to: [input level]
You will receive one input: a level value (int).
This challenge focuses purely on defining interfaces—the blueprints that describe capabilities. In the next lesson, you'll learn how classes actually implement these interfaces to provide the real behavior!
Cheat sheet
An interface is a contract that defines what a class can do, without specifying how it does it. Interfaces contain only method signatures with no implementation.
Interfaces are declared using the interface keyword:
public interface Drawable {
void draw();
}All methods in an interface are implicitly public and abstract:
public interface Playable {
void play(); // implicitly public abstract
void pause();
void stop();
}Interfaces can declare constants, which are implicitly public static final:
public interface GameConstants {
int MAX_PLAYERS = 4; // implicitly public static final
}A class can implement multiple interfaces, unlike extending multiple classes.
Try it yourself
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int level = scanner.nextInt();
// TODO: Print information about the interfaces
// Print: "Switchable methods: turnOn, turnOff"
// Print: "Adjustable methods: setLevel"
// Print: "Max level constant: " followed by Adjustable.MAX_LEVEL
// Print: "Setting level to: " followed by the input level
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System