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
Interface declaration
π Scenario: You are creating a simple program to represent different types of vehicles. Each vehicle should be able to start and stop. To ensure all vehicles have these abilities, you will use an interface.
π― Goal: Build a Java interface called Vehicle with two methods: start() and stop(). Then create a class that implements this interface.
π What You'll Learn
Create an interface named Vehicle
Declare two methods in Vehicle: start() and stop()
Create a class named Car that implements the Vehicle interface
Implement the start() and stop() methods in the Car class
Print messages inside start() and stop() methods to show when they are called
π‘ Why This Matters
π Real World
Interfaces help programmers design clear contracts for what classes should do, like vehicles having start and stop actions.
πΌ Career
Understanding interfaces is essential for Java developers to write flexible and maintainable code, especially in large projects and frameworks.
Progress0 / 4 steps
1
Create the Vehicle interface
Create an interface called Vehicle with two method declarations: void start() and void stop().
Java
Hint
An interface only declares methods without bodies. Use void return type and no method body.
2
Create the Car class implementing Vehicle
Create a class called Car that implements the Vehicle interface.
Java
Hint
Use implements Vehicle after the class name. Add empty method bodies for now.
3
Implement start() and stop() methods
Inside the Car class, implement the start() method to print "Car started" and the stop() method to print "Car stopped".
Java
Hint
Use System.out.println() to print messages inside the methods.
4
Test the Car class methods
Create a Main class with a main method. Inside main, create a Car object and call its start() and stop() methods.
Java
Hint
Use System.out.println() to print inside start() and stop(). Then call these methods on the Car object in main.
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();
}
}