Bird
Raised Fist0
Javaprogramming~20 mins

Default methods 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
πŸŽ–οΈ
Default Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of default method call in interface
What is the output of this Java code?
Java
interface Speaker {
    default void sayHello() {
        System.out.println("Hello from Speaker");
    }
}

class Person implements Speaker {
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.sayHello();
    }
}
ACompilation error: sayHello() not implemented
BNo output
CRuntime error: NullPointerException
DHello from Speaker
Attempts:
2 left
πŸ’‘ Hint
Remember that default methods in interfaces provide an implementation that classes can inherit.
❓ Predict Output
intermediate
2:00remaining
Output when overriding default method
What will be printed when running this code?
Java
interface Printer {
    default void print() {
        System.out.println("Printing from Printer");
    }
}

class Document implements Printer {
    public void print() {
        System.out.println("Printing from Document");
    }
}

public class Main {
    public static void main(String[] args) {
        Document doc = new Document();
        doc.print();
    }
}
APrinting from Printer
BPrinting from Document
CRuntime error: MethodNotFoundException
DCompilation error: print() must be public
Attempts:
2 left
πŸ’‘ Hint
Check if the class overrides the default method.
❓ Predict Output
advanced
2:00remaining
Resolving conflict between two default methods
What is the output of this code?
Java
interface A {
    default void show() {
        System.out.println("A show");
    }
}

interface B {
    default void show() {
        System.out.println("B show");
    }
}

class C implements A, B {
    public void show() {
        A.super.show();
    }
}

public class Main {
    public static void main(String[] args) {
        C c = new C();
        c.show();
    }
}
ACompilation error: class C must override show()
BB show
CA show
DRuntime error: AmbiguousMethodException
Attempts:
2 left
πŸ’‘ Hint
When two interfaces have the same default method, the implementing class must resolve the conflict explicitly.
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error in default method usage
This code does not compile. What is the cause of the compilation error?
Java
interface X {
    default void doSomething() {
        System.out.println("Doing something");
    }
}

class Y implements X {
    public void doSomething() {
        System.out.println("Doing something else");
    }
}

public class Main {
    public static void main(String[] args) {
        Y y = new Y();
        y.doSomething();
    }
}
AMethod doSomething() in class Y must be public to override interface method
BInterface X cannot have default methods
CClass Y must be abstract
DMain class must implement interface X
Attempts:
2 left
πŸ’‘ Hint
Check the access modifier of the overriding method compared to the interface method.
🧠 Conceptual
expert
2:00remaining
Behavior of default methods with multiple inheritance and diamond problem
Consider interfaces I1 and I2 both have a default method foo(). Class D implements both interfaces but does NOT override foo(). What happens when foo() is called on an instance of D?
ACompilation error due to diamond problem; class D must override foo()
BRuntime error due to ambiguous method call
CThe foo() method from I2 is called by default
DThe foo() method from I1 is called by default
Attempts:
2 left
πŸ’‘ Hint
Java requires explicit resolution when multiple interfaces provide the same default method.

Practice

(1/5)
1. What is the main purpose of default methods in Java interfaces?
easy
A. To enable multiple inheritance of classes
B. To declare abstract methods that must be implemented by classes
C. To create private helper methods inside interfaces
D. To allow interfaces to have method bodies without breaking existing implementations

Solution

  1. Step 1: Understand interface method rules before Java 8

    Interfaces could only declare abstract methods without bodies, forcing all implementing classes to define them.
  2. Step 2: Role of default methods

    Default methods allow interfaces to provide method bodies, so new methods can be added without breaking existing classes.
  3. Final Answer:

    To allow interfaces to have method bodies without breaking existing implementations -> Option D
  4. Quick Check:

    Default methods add bodies to interfaces safely [OK]
Hint: Default methods add code to interfaces without breaking old classes [OK]
Common Mistakes:
  • Confusing default methods with abstract methods
  • Thinking default methods enable multiple inheritance of classes
  • Believing default methods are private helper methods
2. Which of the following is the correct syntax to declare a default method in a Java interface?
easy
A. default void show() { System.out.println("Hello"); }
B. void default show() { System.out.println("Hello"); }
C. void show() default { System.out.println("Hello"); }
D. default show() void { System.out.println("Hello"); }

Solution

  1. Step 1: Recall default method syntax

    Default methods start with the keyword default, followed by the return type, method name, and body.
  2. Step 2: Check each option

    default void show() { System.out.println("Hello"); } matches the correct syntax: default void show() { ... }. Others have incorrect order or keywords.
  3. Final Answer:

    default void show() { System.out.println("Hello"); } -> Option A
  4. Quick Check:

    default + return type + method name + body = correct syntax [OK]
Hint: default keyword comes before return type in interface methods [OK]
Common Mistakes:
  • Placing default keyword after return type
  • Using default as a method modifier incorrectly
  • Omitting method body for default methods
3. What will be the output of the following code?
interface A {
    default void greet() {
        System.out.println("Hello from A");
    }
}

class B implements A {
    public void greet() {
        System.out.println("Hello from B");
    }
}

public class Test {
    public static void main(String[] args) {
        A obj = new B();
        obj.greet();
    }
}
medium
A. Hello from B
B. Hello from A
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand method overriding with default methods

    Class B overrides the default greet() method from interface A with its own implementation.
  2. Step 2: Determine which greet() is called

    At runtime, the overridden method in class B is called, printing "Hello from B".
  3. Final Answer:

    Hello from B -> Option A
  4. Quick Check:

    Overridden method in class wins [OK]
Hint: Class method overrides interface default method [OK]
Common Mistakes:
  • Assuming default method runs instead of overridden
  • Expecting compilation error due to default method
  • Confusing runtime and compile-time behavior
4. Identify the error in the following code snippet:
interface X {
    default void display() {
        System.out.println("X display");
    }
}

interface Y {
    default void display() {
        System.out.println("Y display");
    }
}

class Z implements X, Y {
    public void display() {
        // ???
    }
}

What should be done inside class Z's display() method to fix the error?
medium
A. Remove one interface from implements list
B. Call X.super.display() or Y.super.display() to resolve ambiguity
C. Make display() method abstract in class Z
D. No changes needed; code compiles fine

Solution

  1. Step 1: Understand diamond problem with default methods

    Class Z implements two interfaces X and Y, both having default display() methods, causing ambiguity.
  2. Step 2: Resolve ambiguity by overriding and calling specific interface method

    Class Z must override display() and explicitly call one interface's default method using X.super.display() or Y.super.display().
  3. Final Answer:

    Call X.super.display() or Y.super.display() to resolve ambiguity -> Option B
  4. Quick Check:

    Explicit super call fixes default method conflict [OK]
Hint: Use InterfaceName.super.method() to fix default method conflicts [OK]
Common Mistakes:
  • Ignoring ambiguity and expecting code to compile
  • Trying to remove interfaces instead of overriding
  • Making method abstract in a concrete class
5. Given two interfaces with default methods, how can a class implement both and combine their behaviors in a single method?
interface Printer {
    default void print() {
        System.out.println("Printing document");
    }
}

interface Scanner {
    default void print() {
        System.out.println("Scanning document");
    }
}

class MultiFunctionDevice implements Printer, Scanner {
    public void print() {
        // Combine both behaviors here
    }
}

Which code inside print() correctly combines both default methods?
hard
A. super.print();
B. print(); print();
C. Printer.super.print(); Scanner.super.print();
D. Printer.print(); Scanner.print();

Solution

  1. Step 1: Understand calling multiple default methods

    To combine behaviors, the class must explicitly call each interface's default method using InterfaceName.super.method().
  2. Step 2: Check each option

    Printer.super.print(); Scanner.super.print(); correctly calls both default methods. print(); print(); causes infinite recursion. super.print(); is invalid syntax. Printer.print(); Scanner.print(); is invalid because interfaces cannot be called like classes.
  3. Final Answer:

    Printer.super.print(); Scanner.super.print(); -> Option C
  4. Quick Check:

    Use InterfaceName.super.method() to call multiple defaults [OK]
Hint: Call each interface default with InterfaceName.super.method() [OK]
Common Mistakes:
  • Calling method recursively causing stack overflow
  • Using super.print() without interface name
  • Trying to call interface methods like static methods