Hackforge Academy

Category: java

abstract class in Java

Published on 04 May 2026

Explanation

An abstract class in Java is a class that cannot be instantiated and is used as a base class for other classes. It is declared using the 'abstract' keyword and can contain both abstract methods (without implementation) and c oncrete methods (with implementation).

Code:

abstract class Animal {
    abstract void sound();
    void eat() {
        System.out.println("Animal 
eats food");
    }
}

Explanation

Abstract classes are mainly used to achieve abstraction by hiding implementation details and showing only essential features to the user.

Code:

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

Explanation

They help in code reusability by allowing common methods to be defined once in the abstract class and reused by multiple subclasses.

Code:

abstract class Shape {
    void display() {
        System.out.println(
"This is a shape");
    }
    abstract void draw();
}

Explanation

Abstract classes provide a template or blueprint for other classes, ensuring that subclasses implement required methods.

Code:

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

Explanation

They support partial abstraction, meaning some methods can have implementation while others are left abstract for subclasses to define.

Code:

abstract class Vehicle {
    abstract void start();
    void stop() {
        System.out.println("
Vehicle stopped");
    }
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community