US Trends

what is abstract class in java

An abstract class in Java is a special type of class that cannot be instantiated and is meant to be extended by other classes.

What Is Abstract Class in Java? (Quick Scoop)

An abstract class in Java is like a partially built blueprint: it defines common structure and behavior, but leaves some details for child classes to fill in.

  • You declare it using the abstract keyword.
  • You cannot create objects of an abstract class directly (new is not allowed on it).
  • It is usually used as a base class in an inheritance hierarchy.
java

abstract class Animal {
    String name;

    // abstract method (no body)
    abstract void makeSound();

    // concrete method (has body)
    void sleep() {
        System.out.println("Sleeping...");
    }
}

Here, Animal can’t be instantiated, but it gives a common template for all animals.

Key Features (At a Glance)

  • Contains abstract methods (without body) and concrete methods (with body).
  • Can have:
    • Constructors.
* Instance variables (fields).
* Static and final methods.
  • A class with at least one abstract method must be declared abstract.
  • Any subclass must either:
    • Implement all abstract methods, or
    • Itself be declared abstract.

Basic Syntax

java

abstract class ClassName {
    // abstract method
    abstract void methodName();

    // concrete method
    void concreteMethod() {
        System.out.println("Implemented logic here");
    }
}

To use it:

java

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        // Animal a = new Animal(); // ❌ not allowed
        Animal dog = new Dog();      // ✅ allowed (polymorphism)
        dog.makeSound();
        dog.sleep();
    }
}

Why Abstract Class Exists (Intuition)

Imagine you’re designing a system where many classes share some behavior but must customize certain parts.

  • Abstract class lets you:
    • Put shared code in one place (concrete methods).
* Force subclasses to implement specific behaviors (abstract methods).
* Use **polymorphism** , e.g., `Shape s = new Circle();` where `Shape` is abstract.

This is widely used in frameworks and large projects where you define a common contract plus some default behavior.

Abstract Class vs Interface (High-Level)

Even though your question is only about abstract classes, in modern Java people often compare them with interfaces in discussions and forums.

[1][7][3] [7] [1][5][7] [7] [1][7] [7] [2][5] [5][7]
Aspect Abstract Class Interface (modern Java)
Instantiation Cannot be instantiated, must be subclassed. Cannot be instantiated, must be implemented.
Methods Can have abstract + concrete methods. Can have abstract, default, static methods.
State (fields) Can have instance variables, normal fields. Fields are implicitly public static final.
Use case “Is-a” relationship with shared code and partial implementation. Primarily for defining capabilities/contract.

Simple Real-Life Style Example

Think of an abstract class as a base Vehicle :

java

abstract class Vehicle {
    String brand;

    Vehicle(String brand) {
        this.brand = brand;
    }

    abstract void start();  // must be defined by each vehicle

    void displayInfo() {
        System.out.println("Brand: " + brand);
    }
}

class Car extends Vehicle {
    Car(String brand) {
        super(brand);
    }

    @Override
    void start() {
        System.out.println("Starting the car...");
    }
}

Here:

  • Vehicle gives a constructor and a concrete displayInfo() method.
  • start() is abstract, so every specific vehicle must define its own way of starting.

Small Forum-Style Clarification

“So… is an abstract class just an interface with some code?”

Not exactly:

  • An abstract class is often described as a blueprint with some ready-made parts.
  • An interface is more like a pure contract (though default methods blur the line in newer Java).

On Q&A forums, devs often say: use an abstract class when sharing code and state, and use interfaces for capabilities or multiple “roles” a class can play.

Quick Summary (TL;DR)

  • Abstract class = class declared with abstract that cannot be instantiated.
  • Can have both abstract and non-abstract methods, plus fields and constructors.
  • Used as a base to enforce a structure and share code across subclasses.

Note: Information gathered from public forums or data available on the internet and portrayed here.