---
title: "Abstraction and Interfaces in Java"  
description: "Use an abstract class when you need shared fields or partially implemented methods, and an interface when you need a strict contract without implement"  
author: "Ashutosh Patel"  
published: 2025-03-19  
updated: 2025-03-19  
canonical: https://www.mindstick.com/articles/338811/abstraction-and-interfaces-in-java  
category: "java"  
tags: ["java", "interface", "abstraction"]  
reading_time: 3 minutes  

---

# Abstraction and Interfaces in Java

## Abstraction in Java

Abstraction is a fundamental concept in object-[oriented programming](https://www.mindstick.com/interview/23571/what-is-the-difference-between-object-oriented-programming-and-object-based-programming) (OOP) that hides implementation details and shows only the [essential features](https://answers.mindstick.com/qa/101965/what-are-the-essential-features-of-a-smart-home-hub) to the user. It helps to reduce complexity and increase reusability.

In Java, abstraction is achieved by using the below techniques,

- Abstract Class
- Interface

## Abstract Class

- An abstract class is a class that cannot be instantiated.
- It can have [abstract methods](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression) (without implementation) and concrete methods (with implementation).
- It is used when some methods have the same behavior but some methods need to be implemented differently by subclasses.

## Example-

```java
class Main {
   public static void main(String[] args) {
       Vehicle cars = new Car();
       cars.start();
       cars.stop();
   }
}
// Abstract class
abstract class Vehicle {
   // Abstract method (no implementation)
   abstract void start();
   // Concrete method (implemented)
   void stop() {
       System.out.println("Vehicle is stopping...");
   }
}
// Subclass providing implementation for abstract method
class Car extends Vehicle {
   @Override
   void start() {
       System.out.println("Car is starting with a key...");
   }
}
```

## Key Points of Abstract Class

- It can have both abstract and concrete methods.
- It can have constructors and [instance variables](https://www.mindstick.com/forum/34631/instance-variables-and-class-variables).
- It can have final, static and non-static methods.
- It cannot be instantiated directly.
- It can be inherited using extends keyword.

## Interfaces in Java

**Interface** in Java is a **fully abstract** class that contains only abstract methods (before Java 8) and default/static methods (since Java 8). It is used to **achieve 100% abstraction** and [multiple inheritance](https://www.mindstick.com/forum/2519/java-multiple-inheritance).

```java
public class Main {
   public static void main(String[] args) {
       Animal myDog = new Dog(); // Upcasting
       myDog.makeSound(); // Output: Dog barks...
   }
}
// Interface
interface Animal {
   void makeSound(); // Abstract method (no implementation)
}
// Implementing the interface
class Dog implements Animal {
   @Override
   public void makeSound() {
       System.out.println("Dog barks...");
   }
}
```

## Key Points of Interface

- It cannot have instance variables.
- All methods are public and abstract by default.
- Supports multiple inheritance (one class can implement [multiple interfaces](https://www.mindstick.com/interview/2599/can-you-inherited-multiple-interfaces)).
- It cannot have constructors.
- It can have [default and static](https://www.mindstick.com/forum/160958/what-are-default-and-static-methods-in-interfaces-in-java) methods (Java 8+).

## Summary

- **Abstraction** helps to design a clean and structured system by hiding unnecessary details.
- **[Abstract classes](https://www.mindstick.com/forum/34146/difference-between-abstract-classes-and-interface-in-php)** provide a way to share code while enforcing rules through abstract methods.

   - **Interfaces** define a contract that [multiple classes](https://www.mindstick.com/forum/159384/how-can-i-select-an-element-with-multiple-classes-in-jquery) can follow, allowing flexibility and multiple inheritance.

- Use **abstract classes** when you need shared fields or partially implemented methods and use **interfaces** when you need a strict contract without implementation

Also, Read: [Understanding Mutable and Immutable Strings in Java, Key Differences and Use Cases](https://www.mindstick.com/articles/336002/understanding-mutable-and-immutable-strings-in-java-key-differences-and-use-cases)

---

Original Source: https://www.mindstick.com/articles/338811/abstraction-and-interfaces-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
