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

Software Engineering · Basic · question 9 of 100

What are the key differences between an interface and an abstract class?

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

In object-oriented programming, interfaces and abstract classes are two mechanisms for defining contracts or blueprints for classes to follow.

First, let’s define what an interface and an abstract class are.

An interface in a programming language defines a set of methods that a class implementing the interface must implement. An interface specifies what methods should exist and what parameters and return types each method should have, but it does not provide any implementation. In other words, an interface is like a contract that guarantees that a certain set of methods are available in a class that implements it.

An abstract class is a class that cannot be instantiated itself, but instead serves as a base class that defines the structure and behavior of its subclasses. An abstract class can have both implemented and unimplemented methods, and its subclasses must override the unimplemented methods. Abstract classes are used to define a common interface for multiple subclasses, while allowing each subclass to provide its own implementation of certain methods.

Now, let’s compare the key differences between an interface and an abstract class:

1. Implementation: An interface only declares method signatures without any implementation, while an abstract class may have implemented and unimplemented methods.

2. Multiple inheritance: In most programming languages, a class can only inherit from one abstract class, but can implement multiple interfaces. This is because an abstract class defines an inheritance hierarchy, while an interface defines a contract only.

3. Constructor: An interface does not have a constructor. In contrast, an abstract class can have a constructor that is called by its subclasses.

4. Access modifiers: An interface’s methods are always public, while an abstract class can have methods with any access modifier.

5. Purpose: An interface is typically used to define a contract for a class to follow, while an abstract class is used to define a common structure and behavior for a group of subclasses.

To illustrate these differences, let’s consider an example of two classes, ‘Dog‘ and ‘Cat‘, that share some common characteristics, but have different behaviors:

interface Animal {
  public void makeSound();
}

abstract class Mammal {
  public void eat() {
    // implement common behavior for all mammals
  }
  public abstract void move(); // allow each subclass to override
}

class Dog extends Mammal implements Animal {
  public void makeSound() {
    // implement Dog's sound
  }
  public void move() {
    // implement Dog's movement
  }
}

class Cat extends Mammal implements Animal {
  public void makeSound() {
    // implement Cat's sound
  }
  public void move() {
    // implement Cat's movement
  }
}

In this example, ‘Animal‘ is an interface that defines a contract for ‘Dog‘ and ‘Cat‘ to make a sound. ‘Mammal‘ is an abstract class that defines a common behavior for all mammals to eat, and requires subclasses to override the ‘move‘ method. Finally, ‘Dog‘ and ‘Cat‘ are subclasses that inherit from ‘Mammal‘ and implement ‘Animal‘ to provide their own implementation of the required methods.

Overall, the key differences between interfaces and abstract classes are in their purpose and implementation details, and understanding these differences can help determine which one to use in a particular situation.

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

All 100 Software Engineering questions · All topics