Bird
Raised Fist0
Javaprogramming~20 mins

Implementing interfaces in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of interface method implementation
What is the output of this Java program that implements an interface?
Java
interface Speaker {
    void speak();
}

class Person implements Speaker {
    public void speak() {
        System.out.println("Hello from Person");
    }
}

public class Main {
    public static void main(String[] args) {
        Speaker s = new Person();
        s.speak();
    }
}
ACompilation error: speak() must be public
BHello from Person
CRuntime error: NullPointerException
DNo output
Attempts:
2 left
πŸ’‘ Hint
Check if the method speak() is correctly implemented with the right access modifier.
❓ Predict Output
intermediate
2:00remaining
Interface reference to different implementations
What will be printed when running this Java code?
Java
interface Animal {
    void sound();
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Woof");
    }
}

class Cat implements Animal {
    public void sound() {
        System.out.println("Meow");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
        a = new Cat();
        a.sound();
    }
}
A
Woof
Meow
B
Woof
Woof
C
Meow
Woof
DCompilation error: cannot assign Cat to Animal
Attempts:
2 left
πŸ’‘ Hint
Look at how the interface reference is assigned to different objects.
πŸ”§ Debug
advanced
2:00remaining
Why does this interface implementation cause a compile error?
Identify the cause of the compile error in this code snippet.
Java
interface Calculator {
    int add(int a, int b);
}

class SimpleCalculator implements Calculator {
    public int add(int x, int y) {
        return x + y;
    }
}
AMethod add() in SimpleCalculator must be declared public
BInterface Calculator cannot have method with parameters
CClass SimpleCalculator must be abstract
DMethod add() must return void
Attempts:
2 left
πŸ’‘ Hint
Check the access modifier of the implemented method compared to the interface method.
πŸ“ Syntax
advanced
2:00remaining
Which code snippet correctly implements multiple interfaces?
Select the code snippet that correctly implements two interfaces in one class.
A
class Multi implements (A, B) {
    public void methodA() {}
    public void methodB() {}
}
B
class Multi implements A & B {
    public void methodA() {}
    public void methodB() {}
}
C
class Multi implements A, B {
    public void methodA() {}
    public void methodB() {}
}
D
class Multi implements A; B {
    public void methodA() {}
    public void methodB() {}
}
Attempts:
2 left
πŸ’‘ Hint
Remember the syntax for implementing multiple interfaces uses commas.
πŸš€ Application
expert
3:00remaining
Determine the output of interface default method override
What is the output of this Java program that uses interface default methods and overrides?
Java
interface Printer {
    default void print() {
        System.out.println("Printing from Printer");
    }
}

interface Scanner {
    default void print() {
        System.out.println("Printing from Scanner");
    }
}

class MultiFunctionMachine implements Printer, Scanner {
    public void print() {
        Printer.super.print();
        Scanner.super.print();
        System.out.println("Printing from MultiFunctionMachine");
    }
}

public class Main {
    public static void main(String[] args) {
        MultiFunctionMachine m = new MultiFunctionMachine();
        m.print();
    }
}
ACompilation error: print() method conflict
BPrinting from MultiFunctionMachine
C
Printing from Printer
Printing from MultiFunctionMachine
D
Printing from Printer
Printing from Scanner
Printing from MultiFunctionMachine
Attempts:
2 left
πŸ’‘ Hint
Look at how the class calls the default methods from both interfaces explicitly.

Practice

(1/5)
1. What keyword does a Java class use to follow an interface?
easy
A. uses
B. extends
C. inherits
D. implements

Solution

  1. 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.
  2. Step 2: Identify the correct keyword

    The keyword to make a class follow an interface is implements, not extends which is for classes inheriting other classes.
  3. Final Answer:

    implements -> Option D
  4. Quick 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
A. class Car extends Vehicle {}
B. class Car implements Vehicle {}
C. interface Car implements Vehicle {}
D. class Car uses Vehicle {}

Solution

  1. Step 1: Identify class and interface keywords

    A class is declared with class, and interfaces with interface. Here, Car is a class, Vehicle is an interface.
  2. Step 2: Use correct syntax for implementing interface

    The class Car must use implements keyword to follow Vehicle interface. So class Car implements Vehicle {} is correct.
  3. Final Answer:

    class Car implements Vehicle {} -> Option B
  4. Quick 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
A. Printing document
B. Compilation error: print() not implemented
C. Runtime error
D. No output

Solution

  1. 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.
  2. 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".
  3. Final Answer:

    Printing document -> Option A
  4. Quick 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
A. No error, code is correct
B. Dog should extend Animal, not implement
C. Method sound() must be public in Dog class
D. Interface Animal cannot have methods

Solution

  1. Step 1: Check method visibility in interface implementation

    Interface methods are implicitly public. When implementing, the method must be declared public in the class.
  2. Step 2: Identify method declaration in Dog class

    Dog's sound() method has default (package-private) visibility, missing public keyword, causing compile error.
  3. Final Answer:

    Method sound() must be public in Dog class -> Option C
  4. Quick 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
A. class Calc implements Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } }
B. class Calc implements Calculator { int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } }
C. class Calc extends Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } }
D. 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); } }

Solution

  1. 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.
  2. 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.
  3. 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 A
  4. Quick 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