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 40 of 100

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

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

In Java, both interfaces and abstract classes are used to define abstractions and provide a level of abstraction to implementers. However, they have some key differences.

Multiple inheritance: One of the major differences between an interface and an abstract class is that an interface can extend multiple other interfaces, while a class can only extend a single abstract class. This makes interfaces more flexible and modular than abstract classes. Implementation: Another difference is that an abstract class can provide implementation for some or all of its methods, while an interface can only define method signatures without implementation. This makes abstract classes more suitable for defining common functionality that can be shared among subclasses, while interfaces are more suitable for defining a common contract that multiple unrelated classes can adhere to. Fields and Constructors: An abstract class can have instance variables, constructors, and other types of methods like a regular class, whereas an interface cannot have any of these. However, an interface can have constants declared in it. Usage: Abstract classes are used when we have a common implementation for related classes, while interfaces are used when unrelated classes need to implement the same behavior.

Here is an example of an interface:

public interface Shape {
    double getArea();
    double getPerimeter();
}

In this example, we define an interface Shape with two methods, getArea() and getPerimeter(). Any class that implements this interface must provide an implementation for these two methods.

Here is an example of an abstract class:

public abstract class Shape {
    protected String color;
    
    public Shape(String color) {
        this.color = color;
    }
    
    public abstract double getArea();
    public abstract double getPerimeter();
    
    public String getColor() {
        return color;
    }


}

In this example, we define an abstract class Shape with two abstract methods getArea() and getPerimeter(). We also define an instance variable color and a constructor that sets the value of this variable. Any class that extends this abstract class must provide an implementation for the two abstract methods, but can also inherit the getColor() method and color instance variable.

In summary, interfaces and abstract classes are used to define abstractions in Java, but they have different capabilities and usage scenarios. Interfaces are more flexible and suitable for defining common contracts, while abstract classes are more suitable for defining common implementations and common functionality. Both interfaces and abstract classes have their own set of rules and limitations for how they can be defined and used.

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