Interfaces let you define a set of actions that different classes can do. Implementing interfaces helps make sure classes follow the same rules.
Implementing interfaces in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Java
interface InterfaceName { returnType methodName(); } class ClassName implements InterfaceName { @Override public returnType methodName() { // method body } }
Use the implements keyword to say a class follows an interface.
All methods in an interface must be implemented in the class.
Examples
Animal with a method sound. The class Dog implements it and defines the sound.Java
interface Animal { void sound(); } class Dog implements Animal { @Override public void sound() { System.out.println("Woof"); } }
Vehicle interface has two methods. Car class implements both methods.Java
interface Vehicle { void start(); void stop(); } class Car implements Vehicle { @Override public void start() { System.out.println("Car started"); } @Override public void stop() { System.out.println("Car stopped"); } }
Sample Program
This program defines an interface Printer with a method print. Two classes implement it differently. The Main class creates objects and calls their print methods.
Java
interface Printer { void print(); } class InkjetPrinter implements Printer { @Override public void print() { System.out.println("Printing with Inkjet Printer"); } } class LaserPrinter implements Printer { @Override public void print() { System.out.println("Printing with Laser Printer"); } } public class Main { public static void main(String[] args) { Printer inkjet = new InkjetPrinter(); Printer laser = new LaserPrinter(); inkjet.print(); laser.print(); } }
Important Notes
Interfaces cannot have method bodies (except default or static methods in newer Java versions).
A class can implement multiple interfaces separated by commas.
Use @Override to help catch mistakes when implementing methods.
Summary
Interfaces define what methods a class must have.
Use implements keyword for a class to follow an interface.
All interface methods must be implemented in the class.
Practice
1. What keyword does a Java class use to follow an interface?
easy
Solution
Step 1: Understand Java class and interface relationship
In Java, a class follows an interface by using a specific keyword to promise it will provide all methods declared in the interface.Step 2: Identify the correct keyword
The keyword to make a class follow an interface isimplements, notextendswhich is for classes inheriting other classes.Final Answer:
implements -> Option DQuick Check:
Class follows interface = implements [OK]
Hint: Remember: classes use implements for interfaces, extends for classes [OK]
Common Mistakes:
- Using extends instead of implements for interfaces
- Confusing inherits keyword which doesn't exist in Java
- Using uses keyword which is invalid
2. Which of the following is the correct syntax to declare a class
Car that implements interface Vehicle?easy
Solution
Step 1: Identify class and interface keywords
A class is declared withclass, and interfaces withinterface. Here, Car is a class, Vehicle is an interface.Step 2: Use correct syntax for implementing interface
The class Car must useimplementskeyword to follow Vehicle interface. Soclass Car implements Vehicle {}is correct.Final Answer:
class Car implements Vehicle {} -> Option BQuick Check:
Class + implements + Interface = correct syntax [OK]
Hint: Class implements interface with 'implements' keyword [OK]
Common Mistakes:
- Using extends instead of implements for interfaces
- Declaring class as interface
- Using invalid keyword uses
3. What will be the output of this code?
interface Printer {
void print();
}
class Document implements Printer {
public void print() {
System.out.println("Printing document");
}
}
public class Main {
public static void main(String[] args) {
Printer p = new Document();
p.print();
}
}medium
Solution
Step 1: Check if interface method is implemented
The interface Printer declares method print(). The class Document implements Printer and provides public void print() method, so no error.Step 2: Trace main method execution
Main creates Printer reference p to new Document object and calls p.print(). This calls Document's print() which prints "Printing document".Final Answer:
Printing document -> Option AQuick Check:
Implemented method runs and prints output [OK]
Hint: Implemented interface methods run normally when called [OK]
Common Mistakes:
- Forgetting to make print() public causes compile error
- Assuming interface methods run automatically without implementation
- Confusing runtime error with compile error
4. Identify the error in this code:
interface Animal {
void sound();
}
class Dog implements Animal {
void sound() {
System.out.println("Bark");
}
}medium
Solution
Step 1: Check method visibility in interface implementation
Interface methods are implicitly public. When implementing, the method must be declared public in the class.Step 2: Identify method declaration in Dog class
Dog's sound() method has default (package-private) visibility, missing public keyword, causing compile error.Final Answer:
Method sound() must be public in Dog class -> Option CQuick Check:
Interface methods require public implementation [OK]
Hint: Interface methods must be public in implementing class [OK]
Common Mistakes:
- Omitting public keyword on implemented methods
- Using extends instead of implements for interfaces
- Thinking interface methods can be private
5. Given interface
Calculator with methods add(int a, int b) and subtract(int a, int b), which class correctly implements it to return the sum and difference respectively?hard
Solution
Step 1: Check method signatures and visibility
Interface methods are public and return int. So implementing methods must be public and return int with same parameters.Step 2: Analyze each option
class Calc implements Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } matches signatures exactly with public int return type. class Calc implements Calculator { int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } } misses public keyword. class Calc extends Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } uses extends which is invalid for interfaces. class Calc implements Calculator { public void add(int a, int b) { System.out.println(a + b); } public void subtract(int a, int b) { System.out.println(a - b); } } changes return type to void, which is incorrect.Final Answer:
class Calc implements Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } -> Option AQuick Check:
Match method signatures exactly with public and return type [OK]
Hint: Implemented methods must match interface signatures exactly [OK]
Common Mistakes:
- Omitting public keyword on methods
- Using extends instead of implements for interfaces
- Changing return types or parameters
