---
title: "What is the difference between an abstract class and an interface in Java?"  
description: "What is the difference between an abstract class and an interface in Java?"  
author: "Ravi Vishwakarma"  
published: 2024-07-18  
updated: 2024-07-18  
canonical: https://www.mindstick.com/interview/33953/what-is-the-difference-between-an-abstract-class-and-an-interface-in-java  
category: "java"  
tags: ["java", "javac"]  
reading_time: 4 minutes  

---

# What is the difference between an abstract class and an interface in Java?

#### Abstract Class

An [**abstract class**](https://www.mindstick.com/forum/158922/describe-the-difference-between-abstract-classes-and-interfaces-in-c-sharp) is a class that cannot be instantiated on its own and may contain abstract methods (methods without a body) and non-abstract methods (methods with a body).

- **Usage:** Used when you want to provide a common base class for other classes to extend, while also providing some shared implementation.

## Example

```java
public abstract class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
```

#### Interface

An [**interface**](https://www.mindstick.com/forum/160400/how-to-implement-a-generic-interface-in-c-sharp-provide-an-example) is a reference type in Java that is used to specify a set of abstract methods that a class must implement. From Java 8 onwards, interfaces can also contain default and static methods with implementations.

- **Usage:** Used to define a contract that other classes must follow. Interfaces are typically used to represent capabilities or behaviors that can be shared across different classes.

## Example

```java
public interface Animal {
    void makeSound(); // Abstract method

    default void eat() {
        System.out.println("This animal eats food."); // Default method
    }
}
```

#### Key Differences

| Feature | Abstract Class | Interface |
| --- | --- | --- |
| **Methods** | Can have both abstract and concrete methods | Can have abstract methods, default methods, and static methods |
| **Fields** | Can have instance variables | Can only have public static final fields (constants) |
| **Inheritance** | Supports single inheritance | Supports multiple inheritance |
| **Constructors** | Can have constructors | Cannot have constructors |
| **When to Use** | Use when classes share a common base class | Use to define a contract that multiple classes can implement |

## Read more

[**When to use Interface?**](https://www.mindstick.com/forum/156722/when-to-use-interface)

[**What's the difference between an Abstract Class and an Interface in Java?**](https://www.mindstick.com/forum/95339/what-s-the-difference-between-an-abstract-class-and-interface-in-java)

[**What is the purpose of an interface in OOP?**](https://www.mindstick.com/forum/158817/what-is-the-purpose-of-an-interface-in-oop)

## Answers

### Answer by Ravi Vishwakarma

#### Abstract Class

An [**abstract class**](https://www.mindstick.com/forum/158922/describe-the-difference-between-abstract-classes-and-interfaces-in-c-sharp) is a class that cannot be instantiated on its own and may contain abstract methods (methods without a body) and non-abstract methods (methods with a body).

- **Usage:** Used when you want to provide a common base class for other classes to extend, while also providing some shared implementation.

## Example

```java
public abstract class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
```

#### Interface

An [**interface**](https://www.mindstick.com/forum/160400/how-to-implement-a-generic-interface-in-c-sharp-provide-an-example) is a reference type in Java that is used to specify a set of abstract methods that a class must implement. From Java 8 onwards, interfaces can also contain default and static methods with implementations.

- **Usage:** Used to define a contract that other classes must follow. Interfaces are typically used to represent capabilities or behaviors that can be shared across different classes.

## Example

```java
public interface Animal {
    void makeSound(); // Abstract method

    default void eat() {
        System.out.println("This animal eats food."); // Default method
    }
}
```

#### Key Differences

| Feature | Abstract Class | Interface |
| --- | --- | --- |
| **Methods** | Can have both abstract and concrete methods | Can have abstract methods, default methods, and static methods |
| **Fields** | Can have instance variables | Can only have public static final fields (constants) |
| **Inheritance** | Supports single inheritance | Supports multiple inheritance |
| **Constructors** | Can have constructors | Cannot have constructors |
| **When to Use** | Use when classes share a common base class | Use to define a contract that multiple classes can implement |

## Read more

[**When to use Interface?**](https://www.mindstick.com/forum/156722/when-to-use-interface)

[**What's the difference between an Abstract Class and an Interface in Java?**](https://www.mindstick.com/forum/95339/what-s-the-difference-between-an-abstract-class-and-interface-in-java)

[**What is the purpose of an interface in OOP?**](https://www.mindstick.com/forum/158817/what-is-the-purpose-of-an-interface-in-oop)


---

Original Source: https://www.mindstick.com/interview/33953/what-is-the-difference-between-an-abstract-class-and-an-interface-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
