WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Core Java · Intermediate · question 21 of 100

What is the difference between an abstract class and an interface in Java?

📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

In Java, both abstract classes and interfaces can be used to define a set of methods that must be implemented by a class. However, there are some important differences between the two:

Method implementation: An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods. An abstract method is a method that has no implementation, while a non-abstract method has a concrete implementation. This means that an abstract class can provide some default behavior for its methods, while an interface only defines the signature of its methods.

Multiple inheritance: A class can implement multiple interfaces, but it can only inherit from one abstract class. This means that interfaces can be used to define multiple behaviors that a class can implement, while abstract classes are used to define a common behavior for a group of related classes.

Access modifiers: An abstract class can have public, protected, or private access modifiers for its methods and variables, while an interface can only have public access modifiers for its methods and variables.

Constructors: An abstract class can have constructors, while an interface cannot.

Here’s an example of an abstract class:

public abstract class Animal {
    public abstract void makeSound();

    public void sleep() {
        System.out.println("zzz");
    }
}

public class Dog extends Animal {
    public void makeSound() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.makeSound(); // Output: "Woof!"
        animal.sleep(); // Output: "zzz"
    }
}

In this example, we define an abstract class called Animal that has an abstract method called makeSound() and a non-abstract method called sleep(). We also define a concrete subclass of Animal called Dog that implements the makeSound() method.

We can then create an instance of Dog and store it in a variable of type Animal. Because Dog is a subclass of Animal, it can be treated as an instance of Animal. When we call the makeSound() and sleep() methods on the animal variable, the methods defined in Dog and Animal are called, respectively.

Here’s an example of an interface:

public interface Drawable {
    public void draw();
}

public class Circle implements Drawable {
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

public class Square implements Drawable {
    public void draw() {
        System.out.println("Drawing a square");
    }
}

public class Main {
    public static void main(String[] args) {
        Drawable circle = new Circle();
        Drawable square = new Square();
        circle.draw(); // Output: "Drawing a circle"
        square.draw(); // Output: "Drawing a square"
    }
}

In this example, we define an interface called Drawable that has a single abstract method called draw(). We also define two classes called Circle and Square that implement the Drawable interface and provide concrete implementations of the draw() method.

We can then create instances of Circle and Square and store them in variables of type Drawable. When we call the draw() method on the circle and square variables, the methods defined in Circle and Square are called, respectively.

In summary, abstract classes and interfaces can be used to define a set of methods that must be implemented by a class. Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods. Classes can implement

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Core Java interview — then scores it.
📞 Practice Core Java — free 15 min
📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

All 100 Core Java questions · All topics