ReviseAlgo Logo

Abstraction

Abstract Methods

Examine abstract method signature definitions, mandatory subclass overriding contracts, and visibility constraints.

Interview: Focuses on compiler rules for classes containing abstract methods, access modifier constraints (no private/final), and exception rules.

Last Updated: June 13, 2026 10 min read

An Abstract Method is a method declared with the abstract keyword that contains no body. It defines a signature that concrete subclasses must override, establishing a mandatory contract for behavioral extensions.

Body-less Signature

Abstract methods end with a semicolon instead of curly braces. They only declare name, parameters, return type, and throws list.

Compiler Mandate

Any class containing at least one abstract method must be declared abstract. Concrete subclasses must implement it.

Access Modifier Limits

Abstract methods cannot be declared private, final, or static, as they must be dynamically overridden.

Compiler Requirements for Abstract Methods

The Java compiler enforces several strict rules on abstract method declarations:

  • Class Declaration: If a class contains an abstract method, the class header must include the abstract keyword. Failure to do so yields a compile error: "Class must either be declared abstract or implement abstract method".
  • Subclass Contracts: Any concrete (non-abstract) subclass extending the parent class must override and implement all inherited abstract methods. If the subclass cannot or does not implement them, it must also be declared abstract.

Access and Modifier Constraints

Because abstract methods are meant to be overridden, certain modifier combinations are structurally illegal:

  • private abstract: Private methods are not visible to subclasses, making it impossible to override them.
  • final abstract: Final methods cannot be overridden, directly contradicting the purpose of abstract methods.
  • static abstract: Static methods cannot be overridden dynamically (they belong to classes, not objects), which violates polymorphic execution.

Common Pitfalls

  • Writing Method Bodies: Accidentally adding curly braces (e.g. public abstract void run() {}) to an abstract method declaration, resulting in a compiler error.
  • Declaring abstract methods static: Attempting to create class-level utility abstract methods, which are not allowed by compiler design.

Best Practices

  • Keep Scope Broad: Declare abstract methods as protected or public to allow subclasses from other packages to implement them.
  • Document the Contract: Use Javadocs on the abstract method to define expected inputs, bounds, and output contracts for developers implementing subclasses.

Interview-Relevant Information

Q1: Can we declare an abstract method inside a non-abstract class?
Answer: No. If a class contains any abstract method, the class itself must be declared abstract. Otherwise, the compiler will fail to build the class.

Q2: Can an abstract method throw checked exceptions? What are the overriding constraints on exceptions?
Answer: Yes, abstract methods can declare a throws clause. When subclassing, the overriding method cannot throw broader or new checked exceptions, but it can throw narrower checked exceptions, fewer checked exceptions, or any unchecked runtime exceptions.

Quick Checklist

Can you write a valid abstract method declaration, explain why private abstract is a contradiction, and identify when a subclass must also be declared abstract? If yes, you understand abstract methods.

Use Cases

Defining hook points in framework lifecycles where subclasses must inject custom processing steps.

Creating baseline capability templates in library architectures (e.g. Serializer draw/read steps).

Common Mistakes

Including a method body (empty or not) in abstract method definitions.

Using incompatible access modifiers like private, final, or static on abstract methods.