# Abstract Classes and Interfaces in Java — Differences and When to Use Each

Source: https://skills101.in/learn/java/java-abstract-classes-and-interfaces — Skills101 free tutorial (Java Programming). Updated 2026-11-09.

**Abstraction** means showing *what* something does and hiding *how*. You use a phone's "call" button without knowing the radio protocols. Java gives two tools for abstraction: **abstract classes** and **interfaces**.

![Comparison: abstract class — keyword abstract class, abstract and normal methods, any fields, has constructors, a class extends one, used for is-a with shared code. Interface — abstract, default and static methods, only constants, no constructor, a class implements many, used for a can-do ability](https://skills101.in/learn-img/java/abstract-vs-interface.svg)

## 1. Abstract classes

An `abstract` class **can't be instantiated** and may contain **abstract methods** (no body) that subclasses must implement. It can also have normal methods, fields and constructors — shared code for all children.

```java
abstract class Payment {
    double amount;
    Payment(double amount) { this.amount = amount; }

    abstract String pay();                    // each payment type decides how

    String receipt() {                        // shared code
        return "Paid ₹" + amount + " via " + pay();
    }
}

class UpiPayment extends Payment {
    String upiId;
    UpiPayment(double amount, String upiId) { super(amount); this.upiId = upiId; }
    String pay() { return "UPI (" + upiId + ")"; }
}

class CardPayment extends Payment {
    CardPayment(double amount) { super(amount); }
    String pay() { return "card"; }
}

public class Main {
    public static void main(String[] args) {
        Payment[] payments = { new UpiPayment(499, "asha@upi"), new CardPayment(1299) };
        for (Payment p : payments) System.out.println(p.receipt());
        // new Payment(10);  ← compile error: Payment is abstract
    }
}
```

```text
Paid ₹499.0 via UPI (asha@upi)
Paid ₹1299.0 via card
```

## 2. Interfaces

An **interface** is a contract: "any class that implements me promises to have these methods". A class can implement **many** interfaces.

```java
interface Shareable {
    String shareLink();                       // abstract by default
    default String shareText() {              // default method (Java 8+)
        return "Check this out: " + shareLink();
    }
}
interface Downloadable {
    String download();
}

class Certificate implements Shareable, Downloadable {
    String id;
    Certificate(String id) { this.id = id; }
    public String shareLink() { return "skills101.in/certificates/" + id; }
    public String download() { return id + ".pdf"; }
}

public class Main {
    public static void main(String[] args) {
        Certificate c = new Certificate("SK101-42");
        System.out.println(c.shareText());
        System.out.println(c.download());
        Shareable s = c;           // use the object through its interface
        System.out.println(s.shareLink());
    }
}
```

```text
Check this out: skills101.in/certificates/SK101-42
SK101-42.pdf
skills101.in/certificates/SK101-42
```

- Interface methods are `public abstract` by default; fields are `public static final` (constants).
- Implementing methods must be `public`.

## 3. Which one should I use?

- Use an **abstract class** when classes share **state and code** and have an "is-a" relationship (`UpiPayment` is a `Payment`).
- Use an **interface** for a **capability** many unrelated classes can have ("can be shared", "can be compared", "can be run").
- Programming to interfaces (`List<String> list = new ArrayList<>()`) keeps your code flexible.

## 4. Functional interfaces (preview of lambdas)

An interface with **exactly one** abstract method is a **functional interface**. You can implement it with a lambda:

```java
interface Discount {
    double apply(double price);
}

public class Main {
    public static void main(String[] args) {
        Discount festive = p -> p * 0.8;
        Discount flat = p -> p - 100;
        System.out.println(festive.apply(1000) + " " + flat.apply(1000));
    }
}
```

```text
800.0 900.0
```

More in the [Java 8 lesson](https://skills101.in/learn/java/java-8-lambdas-streams).

## Common mistakes

- Trying to create an object of an abstract class or interface with `new`.
- Forgetting to implement every abstract method (the class must then be abstract itself).
- Making implemented interface methods non-public.

## Quick recap

- Abstract class: can't instantiate; abstract + normal methods; fields; constructors; extend one.
- Interface: contract; abstract, default, static methods; constants; implement many.
- Abstract class for shared code ("is-a"); interface for abilities ("can-do").
- One abstract method → functional interface → usable with lambdas.


Practice quiz and free certificate: https://skills101.in/learn/java/java-abstract-classes-and-interfaces