C

Core Java tutorial for beginners

Clean • Professional

Java - Abstraction (Abstract Class, Abstract Method & Interface)

5 minute

What is Abstraction in Java?

Abstraction is one of the four main pillars of Object-Oriented Programming (OOP) — along with Encapsulation, Inheritance, and Polymorphism.

Abstraction means hiding internal implementation details and showing only the essential features of an object.

It allows you to focus on what an object does instead of how it does it.


Real-life Example

Think of driving a car:

  • You press the accelerator to move, brake to stop.
  • You don’t need to know how the engine or gears work internally.

That’s abstraction — only important details are shown to the user.


Why Abstraction?

  • Reduces code complexity
  • Improves security (by hiding implementation)
  • Makes code reusable and maintainable
  • Helps achieve loose coupling

How Abstraction Works in Java

In Java, abstraction can be achieved in two main ways:

learn code with durgesh images

  1. Using Abstract Classes
  2. Using Interfaces

Abstract Class in Java

  • An abstract class is a class declared with the keyword abstract.
  • It can include abstract methods (without body) and concrete methods (with body).
  • You cannot create objects of an abstract class directly.

Syntax

abstract class ClassName {
    abstract void methodName(); // Abstract method (no body)

    void normalMethod() {       // Concrete method
        System.out.println("This is a concrete method");
    }
}

Example – Abstract Class

abstract class Animal {
    abstract void sound(); // Abstract method

    void sleep() {
        System.out.println("Animal is sleeping");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class AbstractClassExample {
    public static void main(String[] args) {
        Animal a = new Dog(); // Reference of abstract class
        a.sound();
        a.sleep();
    }
}

Output:

Dog barks
Animal is sleeping

Something About Abstract Classes

  • Cannot be instantiated directly
  • Can contain both abstract and concrete methods
  • Can have constructors, fields, and static methods
  • Subclasses must override all abstract methods
  • Used for partial abstraction

Abstract Methods in Java

  • An abstract method is declared using the keyword abstract and does not have a body.
  • It defines a method signature, but the actual implementation is provided in subclasses.

Syntax

abstract returnType methodName();

Example – Abstract Method

abstract class Shape {
    abstract void draw(); // Abstract method
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a Circle");
    }
}

class Rectangle extends Shape {
    void draw() {
        System.out.println("Drawing a Rectangle");
    }
}

public class AbstractMethodExample {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.draw();
    }
}

Output:

Drawing a Circle

Interface in Java

  • An interface is a fully abstract type that contains only method declarations (no implementation).
  • It is used to achieve 100% abstraction and multiple inheritance in Java.

Syntax

interface InterfaceName {
    void method1(); // implicitly public and abstract
}

Example – Interface

interface Vehicle {
    void start(); // abstract method
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car starts with a key");
    }
}

class Bike implements Vehicle {
    public void start() {
        System.out.println("Bike starts with a button");
    }
}

public class InterfaceExample {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
    }
}

Output:

Car starts with a key

Interface – Java 8 Enhancements

From Java 8 onwards, interfaces can also have:

  • default methods → with implementation
  • static methods → called using the interface name

Example – Default & Static Methods in Interface

interface Test {
    void show(); // abstract method

    default void display() {
        System.out.println("Default method in interface");
    }

    static void print() {
        System.out.println("Static method in interface");
    }
}

class Example implements Test {
    public void show() {
        System.out.println("Implemented show() method");
    }
}

public class InterfaceFeatures {
    public static void main(String[] args) {
        Example obj = new Example();
        obj.show();
        obj.display();
        Test.print();
    }
}

Output:

Implemented show() method
Default method in interface
Static method in interface

Diagram Concept – Abstract Class vs Interface

               [Abstract Class: Vehicle]
                   /          \\
         [Car] (extends)     [Bike] (extends)
       - start()             - start()
       - stop()

               [Interface: Payment]
        -----------------------------
        |  pay(amount)             |
        -----------------------------
        /           |              \\
CreditCardPayment  PayPalPayment  UPIPayment

Explanation:

  • Abstract class Vehicle: can have concrete methods (stop()) and abstract methods (start()).
  • Interface Payment: only method declarations; multiple classes implement it.
  • Abstraction hides internal workings from the user.

Difference Between Abstract Class and Interface

FeatureAbstract ClassInterface
Abstraction LevelPartial (0–100%)Full (100%)
MethodsAbstract + ConcreteAbstract, default, static
Access ModifiersCan be any (public, protected, private)All methods are public
VariablesInstance or staticAlways public static final
InheritanceSingle inheritanceMultiple inheritance
Keywordextendsimplements
Use CaseCommon base with shared logicDefine contract or capability

When to Use What

Use CasePrefer
You want to share common codeAbstract Class
You want to define only a contractInterface
You need multiple inheritanceInterface
Partial implementation requiredAbstract Class

Points to Remember

  • Abstraction hides unnecessary implementation details.
  • Achieved via abstract classes (partial) and interfaces (complete).
  • Improves readability, reusability, and maintainability.
  • Widely used in frameworks (like JDBC, Collections, Spring).

Real-World Example – Abstraction in Java

Scenario: Payment System

We have multiple types of payments (Credit Card, PayPal, UPI), but the user only interacts with a generic Payment interface. The implementation details are hidden — classic abstraction.

// Interface for abstraction
interface Payment {
    void pay(double amount); // abstract method
}

// Implementation 1: Credit Card Payment
class CreditCardPayment implements Payment {
    public void pay(double amount) {
        System.out.println("Paid " + amount + " using Credit Card");
    }
}

// Implementation 2: PayPal Payment
class PayPalPayment implements Payment {
    public void pay(double amount) {
        System.out.println("Paid " + amount + " using PayPal");
    }
}

// Implementation 3: UPI Payment
class UPIPayment implements Payment {
    public void pay(double amount) {
        System.out.println("Paid " + amount + " using UPI");
    }
}

// Client code
public class PaymentDemo {
    public static void main(String[] args) {
        Payment payment;

        // User chooses Credit Card
        payment = new CreditCardPayment();
        payment.pay(1000);

        // User switches to PayPal
        payment = new PayPalPayment();
        payment.pay(500);
    }
}

Output:

Paid 1000 using Credit Card
Paid 500 using PayPal

Summary Table

ConceptDescriptionExample
Abstract ClassBase class with both abstract and normal methodsabstract class Shape
Abstract MethodDeclared without a body, implemented in subclassabstract void draw()
InterfaceFully abstract type, supports multiple inheritanceinterface Vehicle
Default MethodMethod with body in interface (Java 8+)default void show()

 

Article 0 of 0