An interface in Java is like a contract that says what methods a class must have, without saying how they work.
Interface declaration in Java
Start learning this pattern below
Jump into concepts and practice - no test required
public interface InterfaceName { // method signatures returnType methodName(parameters); }
Interfaces only declare methods; they do not provide method bodies (except default or static methods in newer Java versions).
All methods in an interface are implicitly public and abstract.
public interface Animal { void eat(); void sleep(); }
interface Vehicle { int getSpeed(); }
public interface Drawable { void draw(); default void info() { System.out.println("Drawable object"); } }
This program defines an interface Printer with a print method. ConsolePrinter class implements Printer and provides the print method. The main method creates a ConsolePrinter and calls print.
public interface Printer { void print(String message); } public class ConsolePrinter implements Printer { public void print(String message) { System.out.println(message); } } public class Main { public static void main(String[] args) { Printer printer = new ConsolePrinter(); printer.print("Hello, Interface!"); } }
Interfaces cannot have instance fields (variables), only constants (static final).
A class can implement multiple interfaces, helping with flexible design.
Since Java 8, interfaces can have default and static methods with bodies.
Interfaces define what methods a class must have, without how they work.
Use interfaces to ensure classes follow certain behaviors.
Interfaces help write flexible and reusable code by separating method declaration from implementation.
Practice
interface in Java?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 DQuick Check:
Interface purpose = declare methods only [OK]
- Thinking interfaces can store variables
- Confusing interfaces with classes
- Believing interfaces create objects
Vehicle in Java?Solution
Step 1: Recall Java interface syntax
Interfaces are declared with the keywordinterfacefollowed 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 AQuick Check:
Correct interface syntax = interface Name {} [OK]
- Adding parentheses after interface name
- Mixing class and interface keywords
- Omitting the 'interface' keyword
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();
}
}Solution
Step 1: Understand interface implementation
The class Dog implements Animal and provides the methodsound()which prints "Bark".Step 2: Trace the main method execution
In main, an Animal reference points to a Dog object, callingsound()prints "Bark".Final Answer:
Bark -> Option CQuick Check:
Interface method called prints 'Bark' [OK]
- Expecting interface to print something
- Missing 'public' in method implementation causing error
- Thinking no output occurs
interface Calculator {
int add(int a, int b) {
return a + b;
}
}Solution
Step 1: Check method body rules in interfaces
In Java, interface methods cannot have bodies unless marked asdefaultorstatic.Step 2: Analyze the given method
The methodaddhas a body but nodefaultorstatickeyword, causing a syntax error.Final Answer:
Interfaces cannot have method bodies unless default or static -> Option AQuick Check:
Method bodies in interfaces need default/static [OK]
- Adding method bodies without default/static
- Thinking method names are restricted
- Ignoring Java naming conventions
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();
}
}What is the output when running
Test?Solution
Step 1: Understand multiple interface implementation
Java allows a class to implement multiple interfaces and must provide all their methods.Step 2: Trace method calls in main
The Document class implements both methods. Callingprint()andshow()prints both messages.Final Answer:
Printing document\nShowing document -> Option BQuick Check:
Multiple interfaces implemented, all methods run [OK]
- Thinking multiple interfaces cause errors
- Forgetting to implement all interface methods
- Expecting only one method to run
