Interview Questions, Answers and Tutorials

Interfaces and abstract classes

Interfaces and abstract classes

Hey there, budding programmers! Today, we’re going to dive into a fascinating world of Java programming called “Interfaces and Abstract Classes.” Don’t worry if these terms sound a bit fancy; I’ll break them down for you in a way that’s easy to understand.

What are Interfaces and Abstract Classes?

In Java, Interfaces and Abstract Classes are tools that help us organize and structure our code. Think of them like blueprints for creating things in the world of programming.

Abstract Classes – The General Blueprint

Let’s start with Abstract Classes. Imagine you want to build different types of vehicles – cars, bikes, and planes. But you know that all vehicles have some common features, like a speed and a way to move. An abstract class is like a general blueprint that says, “Hey, all vehicles should have these basic things.”

abstract class Vehicle {
    int speed;

    abstract void move();

    void setSpeed(int s) {
        speed = s;
        System.out.println("Setting speed to " + s);
    }
}

In this code, Vehicle is our abstract class. It has a speed, a method to set the speed, and an abstract method called move(). This is like saying, “Every vehicle must know how to move, but I won’t tell you exactly how; that’s up to the specific vehicle types.”

Now, when you create a specific vehicle like a car or a bike, you have to say exactly how it should move:

class Car extends Vehicle {
    void move() {
        System.out.println("Car is moving on roads.");
    }
}

class Bike extends Vehicle {
    void move() {
        System.out.println("Bike is moving on two wheels.");
    }
}

Interfaces – The Special Instructions

Interfaces are like a set of special instructions that different classes can follow. Continuing with our vehicle example, think of an interface as a checklist that says, “If you want to be considered a vehicle, you need to do these things.”

interface Drivable {
    void start();

    void stop();
}

Here, we have an interface called Drivable that says any class implementing it must have methods start() and stop(). Now, let’s create a class that follows these instructions:

class ElectricCar extends Vehicle implements Drivable {
    void move() {
        System.out.println("Electric car is moving quietly.");
    }

    public void start() {
        System.out.println("Electric car is starting.");
    }

    public void stop() {
        System.out.println("Electric car is stopping.");
    }
}

In this example, ElectricCar is a specific type of vehicle that can move, start, and stop. It follows both the instructions of the Vehicle abstract class and the Drivable interface.

And there you have it, young programmers! Abstract classes and interfaces are like building blocks that help us create organized and efficient code. Abstract classes give us a general blueprint, while interfaces provide special instructions for specific tasks.

Remember, programming is like building with LEGO blocks; you use different pieces to create amazing things. So, go ahead and explore the world of Java programming with interfaces and abstract classes! Happy coding!