Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An abstract class in Java is a restricted class declared with the abstract modifier that cannot be instantiated directly. It serves as a partial blueprint for subclasses, allowing developers to define a structural contract through abstract methods while optionally providing shared state, constructors, and concrete method implementations.
public abstract class AbstractClassName {
    // Instance variable
    protected int stateVariable;

    // Constructor
    public AbstractClassName(int stateVariable) {
        this.stateVariable = stateVariable;
    }

    // Abstract method (no body, requires implementation in subclass)
    public abstract void abstractMethod();

    // Concrete method (fully implemented)
    public void concreteMethod() {
        System.out.println("State: " + stateVariable);
    }
}

Core Technical Rules

1. Instantiation Restrictions Abstract classes cannot be directly instantiated. Attempting to create a direct instance (e.g., new AbstractClassName();) results in a compilation error. However, the new keyword can be used in conjunction with an abstract class’s constructor to instantiate an anonymous concrete subclass on the fly, provided all abstract methods are implemented inline. Otherwise, the class exists to be extended (extends) by named subclasses. 2. Abstract Methods Methods declared with the abstract keyword lack a method body. Any concrete (non-abstract) subclass that inherits from the abstract class must provide an implementation for all inherited abstract methods. If a subclass does not implement all abstract methods, the subclass itself must be declared abstract. 3. Method Modifiers Abstract methods rely on overriding and dynamic method dispatch. Therefore, an abstract method cannot be declared with modifiers that prevent overriding:
  • private
  • final
  • static
4. Constructors Abstract classes can define constructors. While the abstract class cannot be instantiated directly, its constructors are invoked via super() from the constructor of a concrete subclass or an anonymous inner class. This mechanism is used to initialize the instance variables declared within the abstract class. 5. State and Fields Abstract classes can declare instance variables with any access modifier (private, protected, public, package-private). Unlike interfaces, which only permit public static final fields, abstract classes can maintain mutable instance state.

Syntax and Implementation Mechanics

When a concrete class extends an abstract class, it inherits the state and concrete methods, and satisfies the contract of the abstract methods.
// BaseEntity.java
public abstract class BaseEntity {
    private final String id;

    protected BaseEntity(String id) {
        this.id = id;
    }

    // Must be overridden by concrete subclasses
    protected abstract void processEntity();

    public String getId() {
        return id;
    }
}
// UserEntity.java
public class UserEntity extends BaseEntity {
    private String username;

    public UserEntity(String id, String username) {
        super(id); // Invokes BaseEntity constructor
        this.username = username;
    }

    // Mandatory implementation
    @Override
    protected void processEntity() {
        System.out.println("Processing user: " + username);
    }
}

Structural Constraints (Abstract Class vs. Interface)

From a compiler perspective, abstract classes differ structurally from interfaces in Java:
  • Inheritance Limit: A Java class can extend only one abstract class (single inheritance), whereas it can implement multiple interfaces.
  • Access Modifiers: Abstract classes allow protected and package-private abstract methods. In interfaces, methods without an access modifier are implicitly public (note: since Java 9, interfaces can contain private concrete methods, but abstract interface methods remain implicitly public).
  • Initialization: Abstract classes participate in the instance initialization process via constructors and instance blocks; interfaces do not possess constructors or instance initialization blocks.
Master Java with Deep Grasping Methodology!Learn More