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
Why interfaces are used
π Scenario: Imagine you are building a simple system where different types of devices can be turned on and off. Each device behaves differently, but they all share the ability to be switched on or off.
π― Goal: You will create an interface to define the common behavior of turning devices on and off, then implement this interface in different device classes. This will show why interfaces are useful to ensure different classes share the same behavior.
π What You'll Learn
Create an interface called Switchable with two methods: turnOn() and turnOff()
Create a class called Light that implements Switchable
Create a class called Fan that implements Switchable
In the main method, create objects of Light and Fan and call their turnOn() and turnOff() methods
π‘ Why This Matters
π Real World
Interfaces are used in software to ensure different parts follow the same rules, like how different devices can be controlled similarly.
πΌ Career
Understanding interfaces is important for writing clean, reusable code and working with many Java frameworks and APIs.
Progress0 / 4 steps
1
Create the Switchable interface
Create an interface called Switchable with two methods: void turnOn() and void turnOff().
Java
Hint
An interface defines methods without bodies. Use interface keyword and declare methods without implementation.
2
Create the Light class implementing Switchable
Create a class called Light that implements the Switchable interface. Implement the methods turnOn() and turnOff() to print "Light is ON" and "Light is OFF" respectively.
Java
Hint
Use implements Switchable after the class name. Provide method bodies that print the required messages.
3
Create the Fan class implementing Switchable
Create a class called Fan that implements the Switchable interface. Implement the methods turnOn() and turnOff() to print "Fan is ON" and "Fan is OFF" respectively.
Java
Hint
Similar to Light, implement Fan with the required print statements.
4
Create the Main class and test the devices
Create a class called Main with a main method. Inside main, create objects of Light and Fan. Call their turnOn() and turnOff() methods to show the output.
Java
Hint
Create objects and call the methods to see the messages printed.
Practice
(1/5)
1. Why do Java programmers use interfaces? interface defines methods but no implementation. What is the main reason to use them?
easy
A. To ensure different classes share common method names and can work together
B. To store data like variables and constants
C. To create objects directly from the interface
D. To replace classes completely
Solution
Step 1: Understand what interfaces define
Interfaces declare methods without code, so classes must implement those methods.
Step 2: Recognize the purpose of interfaces
They allow different classes to share method names, enabling them to work together flexibly.
Final Answer:
To ensure different classes share common method names and can work together -> Option A
Quick Check:
Interfaces share method names = A [OK]
Hint: Interfaces define method rules for classes to follow [OK]
Common Mistakes:
Thinking interfaces store data
Believing interfaces create objects
Confusing interfaces with classes
2. Which of the following is the correct way to declare an interface in Java?
easy
A. interface Vehicle { void move(); }
B. class Vehicle { void move(); }
C. interface Vehicle() { void move(); }
D. interface Vehicle { void move() {} }
Solution
Step 1: Check interface declaration syntax
Interfaces use the keyword interface followed by name and method signatures without bodies.
Step 2: Identify correct method declaration
Methods in interfaces have no body, so void move(); is correct.
Final Answer:
interface Vehicle { void move(); } -> Option A
Quick Check:
Interface syntax = A [OK]
Hint: Interface methods have no body, just signatures [OK]
Common Mistakes:
Adding parentheses after interface name
Defining method bodies inside interface
Using class keyword instead of interface
3. What will be the output of this Java 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
Class Dog implements Animal and provides the sound() method printing "Bark".
Step 2: Trace method call
Variable a is Animal type but refers to Dog object, so a.sound() calls Dog's method printing "Bark".
Final Answer:
Bark -> Option C
Quick Check:
Interface method call runs Dog's method = C [OK]
Hint: Interface reference calls implemented method in class [OK]
Common Mistakes:
Expecting interface method to print 'sound'
Thinking code won't compile without method body in interface
Confusing output with method name
4. Find the error in this code snippet:
interface Shape { void draw(); } class Circle implements Shape { void draw() { System.out.println("Circle drawn"); } }
medium
A. Interface cannot have methods
B. No error, code is correct
C. Circle class should be abstract
D. Method draw() must be public in Circle class
Solution
Step 1: Check method visibility rules
Interface methods are implicitly public, so implementing methods must be public too.
Step 2: Identify method visibility in Circle
Method draw() in Circle has default (package-private) visibility, missing public.
Final Answer:
Method draw() must be public in Circle class -> Option D
Quick Check:
Interface methods require public implementation = B [OK]
Hint: Implement interface methods as public always [OK]
Common Mistakes:
Ignoring method visibility mismatch
Thinking interface methods can be private
Assuming no error if method is package-private
5. You want to design a system where multiple unrelated classes like Printer, Scanner, and Camera can all be "Connectable" to a computer. Which approach best uses interfaces to achieve this?
hard
A. Use abstract classes for Printer, Scanner, and Camera instead of interfaces
B. Create a Connectable interface with a connect() method, and have each class implement it
C. Add connect() method directly in each class without interface
D. Make Printer, Scanner, and Camera extend a common class Connectable
Solution
Step 1: Identify the need for shared behavior
All classes need a common method connect() to work with the computer.
Step 2: Use interface for unrelated classes
Interfaces allow unrelated classes to share method names without forcing inheritance.
Step 3: Evaluate options
Create a Connectable interface with a connect() method, and have each class implement it uses interface to define connect(), letting each class implement it as needed.
Final Answer:
Create a Connectable interface with a connect() method, and have each class implement it -> Option B
Quick Check:
Interfaces enable shared methods for unrelated classes = D [OK]
Hint: Use interfaces to share methods across unrelated classes [OK]
Common Mistakes:
Trying to use class inheritance for unrelated classes