DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 3 - abstract Keyword: Abstract Classes, Abstract Methods

The abstract keyword is one of the core concepts of Object-Oriented Programming (OOP) in Java. It allows you to define a common blueprint while leaving the implementation details to child classes.

Many beginners confuse abstract classes, abstract methods, and interfaces. They also struggle with questions like:

  • Why can't we create an object of an abstract class?
  • Why must child classes implement abstract methods?
  • Why are combinations like abstract final illegal?

These are also some of the most frequently asked Java interview questions.

In this article, you'll learn:

  • What the abstract keyword is
  • Abstract methods
  • Abstract classes
  • Rules of abstraction
  • Illegal modifier combinations
  • Common compiler errors
  • Best practices
  • Interview questions

What is the abstract Keyword?

The abstract keyword is a non-access modifier that represents incomplete implementation.

It tells Java:

"The implementation is not complete yet."

The abstract modifier is applicable only to:

  • Classes
  • Methods

It cannot be applied to variables.

Applicable To Allowed?
Class
Method
Variable

Why Do We Need abstract?

Imagine you're building an e-commerce application.

Every payment method must perform a payment.

However, the payment process differs depending on the payment type.

For example:

Payment
│
├── Credit Card
├── UPI
├── Net Banking
└── Wallet
Enter fullscreen mode Exit fullscreen mode

Every payment should have a pay() method.

But how it performs the payment is different.

Instead of writing incomplete code, Java allows us to declare the method as abstract, letting each child class provide its own implementation.


Syntax

Abstract Method

public abstract void methodName();
Enter fullscreen mode Exit fullscreen mode

Notice:

  • No method body
  • Ends with a semicolon

Abstract Class

abstract class Payment {

}
Enter fullscreen mode Exit fullscreen mode

How It Works

Abstract Class
      │
      ▼
Contains Abstract Methods
      │
      ▼
Child Class
      │
      ▼
Provides Implementation
Enter fullscreen mode Exit fullscreen mode

An abstract class acts like a blueprint.

Child classes complete the blueprint.


What is an Abstract Method?

An abstract method has:

  • Declaration
  • Return type
  • Parameters

But no implementation.

Example:

abstract class Payment {

    public abstract void pay();

}
Enter fullscreen mode Exit fullscreen mode

Notice that the declaration ends with a semicolon.


Rule 1: Abstract Methods Have No Body

Correct:

abstract class Payment {

    public abstract void pay();

}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

The compiler reads the method declaration.

Step 2

It sees the abstract keyword.

Step 3

Java understands the implementation is intentionally missing.

Step 4

The child class becomes responsible for implementing the method.


Rule 2: Child Classes Must Implement Abstract Methods

Example:

abstract class Payment {

    public abstract void pay();

}
Enter fullscreen mode Exit fullscreen mode
class UpiPayment extends Payment {

    @Override
    public void pay() {
        System.out.println("Payment completed using UPI");
    }

}
Enter fullscreen mode Exit fullscreen mode

Output

Payment completed using UPI
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

Payment declares the contract.

Step 2

UpiPayment extends Payment.

Step 3

It provides the missing implementation.

Step 4

Compilation succeeds.


Rule 3: An Abstract Method Cannot Have a Body

Incorrect code:

abstract class Payment {

    public abstract void pay() {

    }

}
Enter fullscreen mode Exit fullscreen mode

Compile-Time Error

abstract methods cannot have a body
Enter fullscreen mode Exit fullscreen mode

Why?

An abstract method represents incomplete implementation.

Providing a body contradicts its purpose.


Rule 4: Missing Body Without abstract

Incorrect code:

class Payment {

    public void pay();

}
Enter fullscreen mode Exit fullscreen mode

Compile-Time Error

missing method body, or declare abstract
Enter fullscreen mode Exit fullscreen mode

Why?

A method without a body must be declared abstract.


Rule 5: If a Class Contains an Abstract Method, the Class Must Be Abstract

Incorrect code:

class Payment {

    public abstract void pay();

}
Enter fullscreen mode Exit fullscreen mode

Compile-Time Error

Payment is not abstract and does not override abstract method pay()
Enter fullscreen mode Exit fullscreen mode

Why?

The class contains incomplete implementation.

Therefore, the class itself must be declared abstract.

Correct code:

abstract class Payment {

    public abstract void pay();

}
Enter fullscreen mode Exit fullscreen mode

Rule 6: Abstract Classes Cannot Be Instantiated

Example:

abstract class Payment {

}
Enter fullscreen mode Exit fullscreen mode
public class Main {

    public static void main(String[] args) {

        Payment payment = new Payment();

    }

}
Enter fullscreen mode Exit fullscreen mode

Compile-Time Error

Payment is abstract; cannot be instantiated
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

The compiler sees an attempt to create an object.

Step 2

It checks the class declaration.

Step 3

The class is abstract.

Step 4

Object creation is rejected.


Why Can't We Create Objects of an Abstract Class?

Consider a blueprint of a house.

You can't live inside a blueprint.

First, the house must be built.

Similarly,

An abstract class is only a blueprint.

Only concrete child classes can be instantiated.


Rule 7: Child Classes Must Implement All Abstract Methods

Example:

abstract class Employee {

    public abstract void work();

    public abstract void attendMeeting();

}
Enter fullscreen mode Exit fullscreen mode

Incorrect code:

class SoftwareEngineer extends Employee {

    @Override
    public void work() {

    }

}
Enter fullscreen mode Exit fullscreen mode

Compile-Time Error

SoftwareEngineer is not abstract and does not override abstract method attendMeeting()
Enter fullscreen mode Exit fullscreen mode

Why?

Every inherited abstract method must be implemented.


Rule 8: If the Child Doesn't Implement Everything, It Must Also Be Abstract

Correct code:

abstract class Employee {

    public abstract void work();

    public abstract void attendMeeting();

}
Enter fullscreen mode Exit fullscreen mode
abstract class SoftwareEngineer extends Employee {

    @Override
    public void work() {

    }

}
Enter fullscreen mode Exit fullscreen mode

Compilation succeeds.

Now the responsibility shifts to the next child class.


Abstract Classes Can Have Concrete Methods

An abstract class doesn't have to contain only abstract methods.

Example:

abstract class Payment {

    public abstract void pay();

    public void printReceipt() {
        System.out.println("Receipt Generated");
    }

}
Enter fullscreen mode Exit fullscreen mode

This is very common in enterprise applications.


Can an Abstract Class Have Zero Abstract Methods?

Yes.

This surprises many beginners.

Example:

abstract class Vehicle {

    public void start() {
        System.out.println("Vehicle Started");
    }

}
Enter fullscreen mode Exit fullscreen mode

This compiles successfully.

Real-World Examples

  • HttpServlet
  • Adapter classes in Java libraries

These are abstract classes even though they may not declare any abstract methods.


Illegal Modifier Combinations

Some modifier combinations are illegal because they contradict the meaning of abstract.

Rule

If another modifier talks about implementation, it becomes an enemy of abstract.

Illegal Combination Reason
abstract final Abstract requires overriding, final prevents overriding
abstract static Static methods belong to the class, not subclasses
abstract synchronized synchronized requires implementation
abstract native Native methods have platform-specific implementation
abstract strictfp (method) strictfp specifies implementation details
abstract private Private methods cannot be inherited or overridden

Difference Between Abstract Class and Abstract Method

Abstract Class Abstract Method
Declared using abstract class Declared using abstract
Cannot be instantiated Has no implementation
May contain concrete methods Cannot have a body
May contain zero abstract methods Must be implemented by child classes

Difference Between final and abstract

final abstract
Prevents overriding Requires overriding
Prevents inheritance Encourages inheritance
Cannot contain abstract methods Can contain final methods
Complete implementation Incomplete implementation

Important Rule

Because their purposes are opposite,

abstract final is illegal for both classes and methods.


Common Beginner Mistakes

Mistake 1: Giving a Body to an Abstract Method

Incorrect:

public abstract void pay() {

}
Enter fullscreen mode Exit fullscreen mode

Compile-time error:

abstract methods cannot have a body
Enter fullscreen mode Exit fullscreen mode

Mistake 2: Forgetting to Declare the Class Abstract

Incorrect:

class Payment {

    public abstract void pay();

}
Enter fullscreen mode Exit fullscreen mode

Compile-time error:

Payment is not abstract...
Enter fullscreen mode Exit fullscreen mode

Mistake 3: Instantiating an Abstract Class

Incorrect:

Payment payment = new Payment();
Enter fullscreen mode Exit fullscreen mode

Compile-time error:

Payment is abstract; cannot be instantiated
Enter fullscreen mode Exit fullscreen mode

Mistake 4: Forgetting to Implement Every Abstract Method

This is one of the most common interview mistakes.


Best Practices

  • Use abstract classes when child classes share common state or behavior.
  • Keep abstract methods focused and meaningful.
  • Don't create abstract classes unless you expect inheritance.
  • Prefer interfaces when unrelated classes share only behavior.
  • Use abstract classes to define templates for subclasses.

Interview Questions

1. What is an abstract class?

An abstract class is an incomplete class that cannot be instantiated directly.

Why interviewers ask

To test your understanding of abstraction.

Common trap

Saying abstract classes cannot contain concrete methods.


2. Can an abstract class have constructors?

Yes.

Constructors are executed when child objects are created.


3. Can an abstract class have zero abstract methods?

Yes.

Examples include HttpServlet and many adapter classes.


4. Can we create an object of an abstract class?

No.


5. Why can't abstract methods have a body?

Because they represent incomplete implementation.


6. Can an abstract class contain final methods?

Yes.

Final methods simply cannot be overridden.


7. Is abstract final legal?

No.

The two modifiers have opposite meanings.


8. Can an abstract class contain static methods?

Yes.

Only abstract static methods are illegal.


Quick Memory Trick 🧠

Remember BIC:

B → Blueprint

I → Incomplete Implementation

C → Child Completes It
Enter fullscreen mode Exit fullscreen mode

If you remember BIC, you'll never forget the purpose of the abstract keyword.


Key Takeaways

  • abstract is applicable only to classes and methods.
  • Abstract methods have declarations but no implementation.
  • A class containing an abstract method must be declared abstract.
  • Abstract classes cannot be instantiated.
  • Child classes must implement every inherited abstract method or themselves be declared abstract.
  • Abstract classes can contain concrete methods, constructors, static methods, and even zero abstract methods.
  • abstract final is illegal because the two modifiers have opposite purposes.

What's Next?

In Part 4, we'll cover the strictfp modifier, IEEE 754 floating-point arithmetic, platform-independent calculations, and finish the Java class modifiers series with a complete modifier comparison table.


If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)