Hi! I'm Remsey, your Java instructor. Ready to master Java together?
Prefer to learn by listening? Hit play below and watch the audio come to life!
Today we're diving into something slick, flexible, and powerful in Java β Interfaces.
Letβs imagine youβre building a team of robot sidekicks β like Iron Manβs workshop, but way more chaotic. One can clean, one can draw, and one just spins in circles looking cool. π€
So you think: βHow can I make all these robots guarantee that they follow certain rulesβ¦ but still let them be unique?β
Boom. Java Interfaces to the rescue.
An interface in Java is like a contract. If a class implements that contract, it promises to deliver certain methods. But unlike a class, an interface has no implementation β it just says: βYo, if youβre gonna wear my badge, you better have these methods.β
Let's say we have an interface:
public interface Cleaner {
void clean();
}
Now, two classes implement it:
public class RobotVacuum implements Cleaner {
public void clean() {
System.out.println("Whirrr... I'm cleaning the living room!");
}
}
public class DishwasherBot implements Cleaner {
public void clean() {
System.out.println("Splish splash... dishes are sparkling!");
}
}
In your main() method, you can use them like this:
public class CleaningApp {
interface Cleaner {
void clean();
}
static class RobotVacuum implements Cleaner {
public void clean() {
System.out.println("Whirrr... I'm cleaning the living room!");
}
}
static class DishwasherBot implements Cleaner {
public void clean() {
System.out.println("Splish splash... dishes are sparkling!");
}
}
public static void main(String[] args) {
Cleaner roboVac = new RobotVacuum();
Cleaner dishBot = new DishwasherBot();
roboVac.clean();
dishBot.clean();
System.out.println("All clean! π―");
}
}
Now hereβs where it gets juicy. Java doesnβt allow multiple inheritance with classesβ¦ but with interfaces? Oh baby, go wild. π
Your class can implement as many interfaces as it wants!
public class RoboButler {
interface Cleaner {
void clean();
}
interface Chef {
void cook();
}
interface DJ {
void dropTheBeat();
}
static class SuperBot implements Cleaner, Chef, DJ {
public void clean() {
System.out.println("π§Ή Cleaning the house!");
}
public void cook() {
System.out.println("π¨βπ³ Cooking a 5-star meal!");
}
public void dropTheBeat() {
System.out.println("π΅ DJ SuperBot in the house!");
}
}
public static void main(String[] args) {
SuperBot bot = new SuperBot();
bot.clean();
bot.cook();
bot.dropTheBeat();
System.out.println("β¨ SuperBot does it all!");
}
}
Use interfaces when:
Let's say you have a list of different things: robots, people, etch-a-sketches. If they all
implement a Cleaner interface, you can loop through and call .clean() β
without caring what type they are!
Try completing this challenge by adding a WindowWiperBot class:
public class ChallengeApp {
interface Cleaner {
void clean();
}
static class RobotVacuum implements Cleaner {
public void clean() {
System.out.println("Whirrr... I'm cleaning the living room!");
}
}
static class DishwasherBot implements Cleaner {
public void clean() {
System.out.println("Splish splash... dishes are sparkling!");
}
}
// TODO: Add WindowWiperBot class here that implements Cleaner
// It should print: "Squeaky clean windows coming right up!"
public static void main(String[] args) {
Cleaner roboVac = new RobotVacuum();
Cleaner dishBot = new DishwasherBot();
// Cleaner windowBot = new WindowWiperBot(); // Uncomment after you create the class
roboVac.clean();
dishBot.clean();
// windowBot.clean(); // Uncomment this too!
System.out.println("All clean! π―");
}
}
Test your understanding with these quick exercises! Try to solve them on your own first.
Task: Create a Vehicle interface with
methods start() and stop(). Then create two classes: Car and
Motorcycle that implement this interface.
interface Vehicle to define the contractimplements Vehiclepublic class VehicleDemo {
interface Vehicle {
void start();
void stop();
}
static class Car implements Vehicle {
public void start() {
System.out.println("π Car engine starts: Vroom vroom!");
}
public void stop() {
System.out.println("π Car engine stops.");
}
}
static class Motorcycle implements Vehicle {
public void start() {
System.out.println("ποΈ Motorcycle engine roars to life!");
}
public void stop() {
System.out.println("ποΈ Motorcycle engine shuts off.");
}
}
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle bike = new Motorcycle();
car.start();
bike.start();
car.stop();
bike.stop();
}
}
Task: Create a Payment interface with a
method pay(double amount). Implement it in three classes: CreditCard,
PayPal, and Cash. Each should print a unique payment message.
double parameter"Paid $" + amountpublic class PaymentDemo {
interface Payment {
void pay(double amount);
}
static class CreditCard implements Payment {
public void pay(double amount) {
System.out.println("π³ Paid $" + amount + " with Credit Card");
}
}
static class PayPal implements Payment {
public void pay(double amount) {
System.out.println("π Paid $" + amount + " via PayPal");
}
}
static class Cash implements Payment {
public void pay(double amount) {
System.out.println("π΅ Paid $" + amount + " in Cash");
}
}
public static void main(String[] args) {
Payment creditCard = new CreditCard();
Payment paypal = new PayPal();
Payment cash = new Cash();
creditCard.pay(99.99);
paypal.pay(45.50);
cash.pay(20.00);
}
}
Task: Create an Animal interface with
methods makeSound() and eat(). Create classes for Lion,
Elephant, and Parrot that implement this interface with their unique
behaviors.
public class AnimalDemo {
interface Animal {
void makeSound();
void eat();
}
static class Lion implements Animal {
public void makeSound() {
System.out.println("π¦ Lion roars: ROARRR!");
}
public void eat() {
System.out.println("π¦ Lion is eating meat");
}
}
static class Elephant implements Animal {
public void makeSound() {
System.out.println("π Elephant trumpets: PAWOOOO!");
}
public void eat() {
System.out.println("π Elephant is munching on plants");
}
}
static class Parrot implements Animal {
public void makeSound() {
System.out.println("π¦ Parrot squawks: SQUAWK SQUAWK!");
}
public void eat() {
System.out.println("π¦ Parrot is nibbling on fruits");
}
}
public static void main(String[] args) {
Animal[] zoo = {new Lion(), new Elephant(), new Parrot()};
System.out.println("πͺ Welcome to the Zoo! πͺ\n");
for (Animal animal : zoo) {
animal.makeSound();
animal.eat();
System.out.println();
}
}
}
π‘ Pro Tip: Try solving these exercises without looking at the solutions first! It's the best way to learn. If you get stuck, check the hints before peeking at the solution.
Continue your Java journey with Generics - Type Safety Magic!