Abstraction in Java

Last Updated : 29 Sep, 2026

Abstraction in Java is the process of hiding implementation details and exposing only the essential functionality of an object. It focuses on what an object does rather than how it performs the operation.

  • Reduces complexity by exposing only the necessary features to the user.
  • Achieved primarily using abstract classes and interfaces.
  • Allows implementation details to change without requiring changes to the code that depends on the abstraction.

Ways to Achieve Abstraction

Java provides two ways to implement abstraction, which are listed below:

abstraction

Real-Life Example of Abstraction

A vehicle is a simple real-life example of abstraction. Different types of vehicles can start in different ways, but the common operation can be defined without exposing its implementation details.

Java
abstract class Vehicle {

    // Abstract method
    abstract void start();
}

// Concrete class
class Car extends Vehicle {

    @Override void start()
    {
        System.out.println(
            "Car starts with a key or button.");
    }
}

public class Geeks {

    public static void main(String[] args)
    {

        Vehicle vehicle = new Car();
        vehicle.start();
    }
}

Output
Car starts with a key or button.

Explanation:

  • Vehicle is an abstract class that declares the abstract start() method.
  • Car extends Vehicle and provides the implementation of start().
  • Vehicle vehicle = new Car(); allows the program to use the Vehicle abstraction while the actual implementation is provided by Car.

Abstract class

An abstract class is a class declared using the abstract keyword that cannot be instantiated directly. It can contain both abstract methods (without a body) and concrete methods (with a body), allowing it to provide common functionality while leaving some behavior to subclasses.

  • Can contain abstract and concrete methods.
  • Can have constructors, instance variables, and static members.

Real World Example

The diagram shows an abstract class Shape that contains common properties and methods shared by different shapes. Circle and Rectangle extend Shape and provide their own specific properties, such as radius for Circle and length and width for Rectangle. The area() and toString() methods are abstract, so each concrete class must provide its own implementation.

Abstract Class in Java
Abstract Classes and methods1yh

Example: This program demonstrates partial abstraction by defining an abstract class Shape that includes an abstract method area(), a concrete method getColor(), and a constructor.

Java
abstract class Shape{
    String color;

    // these are abstract methods
    abstract double area();
    public abstract String toString();

    // abstract class can have the constructor
    public Shape(String color){
        
        System.out.println("Shape constructor called");
        this.color = color;
    }

    // this is a concrete method
    public String getColor(){
        
        return color;
        
    }
}
class Circle extends Shape{
    
    double radius;

    public Circle(String color, double radius){

        // calling Shape constructor
        super(color);
        System.out.println("Circle constructor called");
        this.radius = radius;
    }

    @Override double area(){
        
        return Math.PI * Math.pow(radius, 2);
    }

    @Override public String toString(){
        
        return "Circle color is " + super.getColor()
            + "and area is : " + area();
    }
}
class Rectangle extends Shape{

    double length;
    double width;

    public Rectangle(String color, double length,
                     double width)
    {
        // calling Shape constructor
        super(color);
        System.out.println("Rectangle constructor called");
        this.length = length;
        this.width = width;
    }

    @Override double area() { return length * width; }

    @Override public String toString()
    {
        return "Rectangle color is " + super.getColor()
            + "and area is : " + area();
    }
}
public class Test {
    public static void main(String[] args)
    {
        Shape s1 = new Circle("Red", 2.2);
        Shape s2 = new Rectangle("Yellow", 2, 4);

        System.out.println(s1.toString());
        System.out.println(s2.toString());
    }
}

Explanation: In the Shape example, Shape defines the common color property and abstract area() method. Circle and Rectangle extend Shape and provide their own implementations of area(). The Shape reference can refer to different shape objects, hiding their specific implementations.

Interface

An interface in Java defines a contract that specifies the behavior a class should provide. A class implements an interface and provides implementations for its abstract methods. Interfaces can also contain default and static methods with implementations.

  • A class uses the implements keyword to implement an interface.
  • A class can implement multiple interfaces, supporting multiple inheritance of type.

Example: To implement an interface we use the keyword “implements” with class.

Java
// Define an interface named Shape
interface Shape{
    
    // Abstract method for calculating the area
    double calculateArea(); 
}

// Implement the interface 
// in a class named Circle
class Circle implements Shape{
    
    private double r;

    // Constructor for Circle
    public Circle(double r){ 
        
      this.r = r; 
    }

    // Implementing the abstract method 
    // from the Shape interface
    public double calculateArea()
    {
        return Math.PI * r * r;
    }
}

// Implement the interface in a 
// class named Rectangle
class Rectangle implements Shape{
    
    private double length;
    private double width;

    // Constructor for Rectangle
    public Rectangle(double length, double width){
        
        this.length = length;
        this.width = width;
    }

    // Implementing the abstract 
    // method from the Shape interface
    public double calculateArea() { 
      return length * width; 
    }
}

public class Main {
    public static void main(String[] args) {

        // Reference type is the interface (Shape)
        Shape cir = new Circle(5.0);
        Shape rect = new Rectangle(4.0, 6.0);

        // Dynamic method dispatch — decides which method to call at runtime
        System.out.println("Area of Circle: " + cir.calculateArea());
        System.out.println("Area of Rectangle: " + rect.calculateArea());
    }
}

Output
Area of Circle: 78.53981633974483
Area of Rectangle: 24.0

Explanation: In the Shape interface example, Shape declares the calculateArea() method. Circle and Rectangle implement the interface and provide their own area calculations. The Shape references cir and rect allow the program to use both objects through the same interface type.

Comment