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
Recall & Review
beginner
What is an interface in Java?
An interface in Java is a blueprint that defines a set of abstract methods (methods without a body) that a class can implement. It specifies what a class must do, but not how.
Click to reveal answer
beginner
How do you declare an interface in Java?
You declare an interface using the keyword interface followed by the interface name and a block containing method signatures. Example:
public interface MyInterface {
void myMethod();
}
Click to reveal answer
intermediate
Can interfaces have method bodies in Java?
Since Java 8, interfaces can have default methods with bodies using the default keyword, and static methods with bodies. But regular abstract methods do not have bodies.
Click to reveal answer
beginner
What keyword does a class use to implement an interface?
A class uses the <code>implements</code> keyword to promise it will provide code for all the methods declared in the interface.
Click to reveal answer
intermediate
Can an interface extend another interface?
Yes, an interface can extend one or more other interfaces using the extends keyword. This allows combining multiple interfaces into one.
Click to reveal answer
Which keyword is used to declare an interface in Java?
Ainterface
Bclass
Cimplements
Dextends
✗ Incorrect
The keyword interface is used to declare an interface.
How does a class promise to follow an interface's rules?
Aoverrides
Bextends
Cinherits
Dimplements
✗ Incorrect
A class uses implements to promise it will provide all methods from the interface.
Can interfaces have method bodies in Java 8 and later?
AOnly for abstract methods
BNo, never
CYes, for default and static methods
DOnly for private methods
✗ Incorrect
Since Java 8, interfaces can have default and static methods with bodies.
Which of these is true about interfaces?
AInterfaces define method signatures without bodies by default
BInterfaces can contain constructors
CInterfaces cannot extend other interfaces
DInterfaces can be instantiated directly
✗ Incorrect
Interfaces define method signatures without bodies by default.
What keyword allows an interface to inherit from another interface?
Aimplements
Bextends
Cinherits
Duses
✗ Incorrect
Interfaces use extends to inherit from other interfaces.
Explain how to declare an interface and how a class uses it.
Think about the keywords and what each part means.
You got /4 concepts.
Describe the difference between abstract methods and default methods in interfaces.
Consider what changed in Java 8.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of an interface in Java?
easy
A. To inherit code from multiple classes
B. To store data values like variables
C. To create objects directly
D. To declare methods that a class must implement without providing their body
Solution
Step 1: Understand what an interface declares
An interface only declares method signatures without any implementation.
Step 2: Compare with other options
Interfaces do not store data, create objects, or inherit code from classes.
Final Answer:
To declare methods that a class must implement without providing their body -> Option D
Quick Check:
Interface purpose = declare methods only [OK]
Hint: Interfaces declare methods without bodies [OK]
Common Mistakes:
Thinking interfaces can store variables
Confusing interfaces with classes
Believing interfaces create objects
2. Which of the following is the correct way to declare an interface named Vehicle in Java?
easy
A. interface Vehicle {}
B. class Vehicle interface {}
C. interface Vehicle() {}
D. Vehicle interface {}
Solution
Step 1: Recall Java interface syntax
Interfaces are declared with the keyword interface followed by the name and curly braces.
Step 2: Check each option
interface Vehicle {} matches correct syntax: interface Vehicle {}. Others have syntax errors like misplaced parentheses or keywords.
Final Answer:
interface Vehicle {} -> Option A
Quick Check:
Correct interface syntax = interface Name {} [OK]
Hint: Use 'interface Name {}' to declare interfaces [OK]
Common Mistakes:
Adding parentheses after interface name
Mixing class and interface keywords
Omitting the 'interface' keyword
3. What will be the output of the following code?
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Bark");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
medium
A. Compilation error
B. sound
C. Bark
D. No output
Solution
Step 1: Understand interface implementation
The class Dog implements Animal and provides the method sound() which prints "Bark".
Step 2: Trace the main method execution
In main, an Animal reference points to a Dog object, calling sound() prints "Bark".
Final Answer:
Bark -> Option C
Quick Check:
Interface method called prints 'Bark' [OK]
Hint: Implemented method runs when called via interface reference [OK]
Common Mistakes:
Expecting interface to print something
Missing 'public' in method implementation causing error
Thinking no output occurs
4. Identify the error in this interface declaration:
interface Calculator {
int add(int a, int b) {
return a + b;
}
}
medium
A. Interfaces cannot have method bodies unless default or static
B. Method name 'add' is invalid in interfaces
C. Return type 'int' is not allowed in interfaces
D. Interface name must start with lowercase
Solution
Step 1: Check method body rules in interfaces
In Java, interface methods cannot have bodies unless marked as default or static.
Step 2: Analyze the given method
The method add has a body but no default or static keyword, causing a syntax error.
Final Answer:
Interfaces cannot have method bodies unless default or static -> Option A
Quick Check:
Method bodies in interfaces need default/static [OK]
Hint: Interface methods need default/static for bodies [OK]
Common Mistakes:
Adding method bodies without default/static
Thinking method names are restricted
Ignoring Java naming conventions
5. Given two interfaces:
interface Printable {
void print();
}
interface Showable {
void show();
}
class Document implements Printable, Showable {
public void print() {
System.out.println("Printing document");
}
public void show() {
System.out.println("Showing document");
}
}
public class Test {
public static void main(String[] args) {
Document doc = new Document();
doc.print();
doc.show();
}
}