View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Abstract Method in Java

Updated on 03/03/20255,097 Views

In Java, when you need different classes to share common behavior but require each class to provide its own specific implementation, abstract methods come into play.

An abstract method allows you to define the method signature in an abstract class without providing the implementation. The subclasses are then required to implement the method, ensuring a consistent structure while allowing for flexibility in the implementation.

By the end of this tutorial, you'll understand how to define and use abstract methods effectively in Java to create flexible, maintainable code. Let’s dive in!

Improve your Java programming skills with our Software Development courses — take the next step in your learning journey! 

What is an Abstract Method in Java?

An abstract method is a method that is declared without an implementation. It only has the method signature (name, parameters, return type) but no body. The purpose of an abstract method in Java is to define a contract for subclasses, forcing them to provide their own implementation.

Here are its key characteristics:

  • An abstract method has no body.
  • It is declared using the abstract keyword.
  • It must be inside an abstract class or interface.
  • Abstract methods can be called via polymorphism, but only after being implemented in a concrete subclass. 
  • Calling them directly on the abstract class results in a compilation error.

Example:

abstract class Animal {
abstract void sound(); // Abstract method with no body
}

Here, the class Animal declares an abstract method sound(), but it doesn't define how the sound is made. Any subclass of Animal must provide an implementation of this method.

The syntax for defining an abstract method in Java is quite simple:

1. Declare the class as abstract.2. Use the abstract keyword before the method declaration.3. Don't provide a body for the method.

Example with a Class Containing an Abstract Method:

abstract class Shape {
// Abstract method
abstract double area();
}

class Circle extends Shape {
double radius;

Circle(double radius) {
this.radius = radius;
}

// Providing implementation for the abstract method
@Override
double area() {
return Math.PI * radius * radius;
}
}

class Square extends Shape {
double side;

Square(double side) {
this.side = side;
}

// Providing implementation for the abstract method
@Override
double area() {
return side * side;
}
}

public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5); // Creating a Circle object
Shape square = new Square(4); // Creating a Square object

System.out.println("Area of Circle: " + circle.area()); // Output: Area of Circle: 78.53981633974483
System.out.println("Area of Square: " + square.area()); // Output: Area of Square: 16.0
}
}

Explanation:

  • You define an abstract class Shape with an abstract method area() that doesn't have a body.
  • You create two concrete subclasses, Circle and Square, which implement the area() method. 
  • Each subclass provides its own implementation based on the formula for the area of a circle or square.
  • In the main() method, you create instances of Circle and Square, then call the area() method on each. 
  • Since the area() method is implemented differently in each subclass, it calculates the area based on the shape type.

Output:

Area of Circle: 78.53981633974483
Area of Square: 16.0

This example shows how the abstract method area() forces subclasses to define their own logic for calculating the area of different shapes, while maintaining a consistent structure.

Also Read: Exploring the 14 Key Advantages of Java: Why It Remains a Developer's Top Choice in 2025

How Abstract Methods Fit into Abstraction

Abstraction is one of the key principles of object-oriented programming (OOP). It helps hide the complexity of the system while exposing only essential details to the user. 

Abstract methods are a critical part of this principle by providing method signatures without implementation details, leaving the implementation to the subclasses.

Role in Hiding Implementation Details: Abstract method in Java forces subclasses to implement them, ensuring a consistent interface while allowing diverse implementations. This separation allows subclasses to focus on specific behaviors while adhering to a common interface defined by the abstract class.

For example, the abstract method sound() in an Animal class might be used to represent different animals' sounds. However, the actual sound produced (barking for a dog, meowing for a cat) is hidden and left to the subclasses to define.

Example of Hiding Implementation Details:

abstract class Animal {
abstract void sound(); // Abstract method
}

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

class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}

public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();

dog.sound(); // Output: Bark
cat.sound(); // Output: Meow
}
}

Explanation:

  • The Animal class declares the abstract method sound(), but the specific sound each animal makes is not defined in the Animal class.
  • Subclasses Dog and Cat provide their own implementation of the sound() method.
  • The output shows the polymorphic behavior: the same method name sound() behaves differently based on the object type (Dog or Cat).

Output:

Bark
Meow

Also Read: Abstract Class and Methods in Java: Key Concepts, Examples and Best Practices

To see how abstract methods enforce a contract by ensuring subclasses in action, let’s explore how they are defined and used.

How to Define and Use Abstract Methods

In Java, abstract methods are essential for defining a contract that subclasses must adhere to. They allow you to specify method signatures without providing implementations, enabling subclasses to define their specific behaviors. 

This approach promotes a clean and organized code structure, especially when dealing with complex systems.

Abstract Classes and Inheritance: An abstract class in Java is a class that cannot be instantiated directly. It serves as a blueprint for other classes. Abstract classes can contain both abstract methods (methods without a body) and concrete methods (methods with a body). 

To use an abstract class in Java, another class must inherit from it and provide implementations for its abstract methods.

Here’s when to use an abstract class vs. interface:

  • Abstract Class in Java: Abstract classes are used when some shared behavior is needed but should not be instantiated on their own. They are suitable when you want to provide default behavior that can be shared among subclasses.
  • Interface in Java: Use when classes share a common behavior but are otherwise unrelated. Interfaces are ideal for defining a contract that multiple classes can implement, regardless of their position in the class hierarchy.

Example: Abstract Class with an Abstract Method

abstract class Vehicle {
// Abstract method
abstract void startEngine();

// Concrete method
void stopEngine() {
System.out.println("Engine stopped.");
}
}

In this example:

  • Vehicle is an abstract class with an abstract method startEngine() and a concrete method stopEngine().
  • Any class that extends Vehicle must provide an implementation for startEngine() but can use the default implementation of stopEngine().

Also Read: OOPS Concept in Java Explained for Beginners

Difference Between Abstract Classes and Interfaces

While both an interface and abstract class in Java can declare abstract methods, they have key differences that affect their use. 

Before Java 8, all interface methods were implicitly abstract and could not have implementations. Java 8 introduced default and static methods, allowing interfaces to include method bodies, which often leads to confusion. Unlike interfaces, abstract classes can have constructors, instance variables, and non-static methods with implementations, making them better suited for shared behavior with state.

Here’s the difference between interface and abstract class in Java:

Feature

Abstract Classes

Interfaces

Methods

An abstract class can have both abstract and concrete methods, allowing some default behavior while enforcing a contract through abstraction.

Can only declare abstract methods (unless using Java 8+ default methods).

Fields

Can have fields (variables).

Cannot have instance fields (only constants).

Constructors

Can have constructors, which allow initialization of common fields that all subclasses will inherit.

Cannot have constructors.

Inheritance

A class can extend only one abstract class (single inheritance).

A class can implement multiple interfaces (multiple inheritance).

Usage

Typically used for classes that share a common base but have some differences.

Typically used to define common behaviors across unrelated class hierarchies.

Understanding how to use abstract class in Java effectively helps in designing modular, maintainable Java applications.

Also Read: Difference between Abstract Class and Interface

Implementing Abstract Methods in Subclasses

In Java, abstract methods are methods declared without an implementation in an abstract class or interface. Subclasses are required to provide concrete implementations for these methods, ensuring a consistent interface across different classes. 

This mechanism enforces a contract that all subclasses must adhere to, promoting polymorphism and code maintainability.

To implement an abstract method in a subclass:

  • Extend the abstract class: Use the extends keyword to create a subclass.
  • Override the abstract method: Provide a concrete implementation of the abstract method.
  • Use the @Override annotation (optional but recommended): This annotation helps the compiler identify overridden methods and ensures that the method signature matches the one in the superclass.

Code Example:

class Car extends Vehicle {
// Implementing the abstract method
@Override
void startEngine() {
System.out.println("Car engine started.");
}
}

In this example:

  • Car extends Vehicle and provides an implementation for the startEngine() method.
  • The @Override annotation indicates that startEngine() is overriding the abstract method from Vehicle.

Usage:

public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car();
myCar.startEngine(); // Output: Car engine started.
myCar.stopEngine(); // Output: Engine stopped.
}
}

Output:

Car engine started.
Engine stopped.

In this usage:

  • An instance of Car is assigned to a Vehicle reference to demonstrate polymorphism.
  • We call both the overridden startEngine() method and the inherited stopEngine() method.

Note: If a subclass does not provide an implementation for all abstract methods of its superclass, the subclass must also be declared as abstract. citeturn0search17

Also Read: Learn Data Abstraction in Java

Now, let’s look at how abstract methods are widely used in real-world applications, from designing flexible APIs to enforcing standardized behaviors. 

Practical Use Cases of Abstract Methods

Abstract methods in Java are widely used in real-world applications to promote flexibility, reusability, and maintainability. They define method signatures without implementations, allowing subclasses to provide specific behaviors. This approach is commonly used in frameworks, APIs, and various software systems.

Abstract Methods in Real-World Applications

Abstract methods play a crucial role in designing structured and scalable applications. They ensure that different components adhere to a standard interface while allowing for unique implementations.

Common Use Cases:

  • Java libraries like Spring and Hibernate use abstract classes to enforce method structures for developers.
  • Abstract methods allow subclasses to define specific steps in an algorithm while maintaining a consistent flow in the parent class.
  • GUI frameworks define abstract methods for handling user interactions, ensuring customized responses in subclasses.

Example of Abstract Method in a Java Application

Consider an application that calculates the area of different shapes using abstract methods.

Code Example:

// Abstract class Shape
abstract class Shape {
// Abstract method to calculate area
abstract double calculateArea();
}

// Circle class extending Shape
class Circle extends Shape {
private double radius;

Circle(double radius) {
this.radius = radius;
}

@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}

// Rectangle class extending Shape
class Rectangle extends Shape {
private double width;
private double height;

Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

@Override
double calculateArea() {
return width * height;
}
}

// Main class to test implementation
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);

System.out.println("Area of Circle: " + circle.calculateArea());
System.out.println("Area of Rectangle: " + rectangle.calculateArea());
}
}

Explanation:

  • The Shape class contains an abstract method calculateArea().
  • Circle and Rectangle extend Shape and provide their own implementations.
  • The Main class demonstrates polymorphism by treating different shape objects as Shape instances.

Output:

Area of Circle: 78.53981633974483
Area of Rectangle: 24.0

This example illustrates how abstract methods enforce a common structure while allowing flexible, shape-specific implementations.

Also Read: Abstraction vs Encapsulation: Difference Between Abstraction and Encapsulation

Despite their advantages, abstract methods can lead to errors if misused. Let’s explore common mistakes when using abstract methods and how to avoid them.

Common Mistakes with Abstract Methods and How to Avoid Them

Abstract methods in Java are used to enforce method implementation in subclasses. They are declared in an abstract class but do not contain a body. They are useful for enforcing consistent behavior across subclasses, making them ideal for frameworks, template patterns, and API design. 

However, overusing abstraction can lead to unnecessary complexity. If a method’s behavior is unlikely to vary across subclasses, a concrete method may be a better choice. Use abstract methods only when enforcing implementation in subclasses is necessary.

Below is a table highlighting common pitfalls and how to address them:

Mistake

Explanation

How to Avoid

Not Providing an Implementation in Subclass

Abstract methods must be implemented in concrete subclasses unless the subclass is also abstract.

Ensure that all abstract methods are overridden in non-abstract subclasses.

Declaring an Abstract Method in a Non-Abstract Class

Only abstract classes can have abstract methods; otherwise, a compilation error occurs.

Always declare the class as abstract when using abstract methods.

Providing a Method Body for an Abstract Method

Abstract methods cannot have a body since their implementation is deferred to subclasses.

Remove any method body and use only the method signature with a semicolon (;).

Using final, static, or private with Abstract Methods

Abstract methods are meant to be overridden, and these modifiers restrict inheritance or method overriding.

Avoid using final, static, or private with abstract methods. Use protected or public as needed.

Calling an Abstract Method Directly

Abstract methods do not have an implementation and cannot be called directly.

Implement the method in a concrete subclass before calling it.

Forgetting to Mark the Class as Abstract

Java requires an abstract class to contain abstract methods; otherwise, a compilation error occurs.

Always declare the class abstract if it contains abstract methods.

By keeping these pitfalls in mind, developers can ensure efficient use of abstraction in Java and avoid common errors.

Also Read: Careers in Java: How to Make a Successful Career in Java in 2025

Best Practices When Using Abstract Methods in Java

Abstract methods are essential when designing a base class that defines a common interface for all its subclasses. They should be used when:

  • A method must be present in all subclasses but will have different implementations.
  • Enforcing a contract for child classes is necessary to ensure consistency.
  • You are developing a framework where specific behaviors need to be implemented by derived classes.

While abstraction can enhance code organization, excessive abstraction can lead to unnecessary complexity. To avoid this:

  • Do not declare a method abstract unless it has a clear, distinct purpose in derived classes.
  • Consider whether a default implementation in the base class would be more practical.
  • Use abstraction only when it improves maintainability and scalability.

When defining an abstract method:

  • Ensure that the method is declared within an abstract class.
  • Do not provide an implementation for the method in the base class.
  • Follow consistent naming conventions to improve code readability.

Also Read: Abstraction in Java: Types, Examples, and Explanation

To solidify your understanding of Java programming, test your knowledge with this quiz. It’ll help reinforce the concepts discussed throughout the tutorial and ensure you're ready to apply them in your projects.

Quiz to Test Your Knowledge on Abstract Methods in Java

Assess your understanding of abstract methods, best practices, common mistakes, and their implementation in Java by answering the following multiple-choice questions. Dive in!

1. Which of the following is true about abstract methods in Java?

a) They must have a method body.

b) They can be declared inside any class.

c) They can only exist inside an abstract class or interface.

d) They must be private to prevent direct access.

2. What happens if a concrete subclass does not implement all abstract methods from its abstract superclass?

a) The program will compile but throw a runtime error.

b) The subclass must be declared abstract.

c) The subclass can ignore the methods without any issue.

d) The compiler automatically provides a default implementation.

3. Which of the following modifiers cannot be used with an abstract method?

a) protected

b) final

c) public

d) default

4. What will happen if you declare an abstract method inside a non-abstract class?

a) The program will compile successfully.

b) A compilation error will occur.

c) The method will behave as a normal method.

d) The compiler will ignore the abstract keyword.

5. Can an abstract method be static in Java?

a) Yes, because static methods are class-level methods.

b) No, because abstract methods must be overridden in subclasses.

c) Yes, but only in interfaces.

d) No, because static methods cannot be overridden.

6. Why can’t abstract methods be private?

a) Because private methods are not inherited by subclasses.

b) Because abstract methods need to be hidden.

c) Because Java does not allow private methods at all.

d) Because private methods must have a body.

7. What will happen if an abstract class does not contain any abstract methods?

a) It will not compile.

b) It will compile but cannot be instantiated.

c) It will behave like a normal class.

d) The compiler will convert it to an interface.

8. Which of the following is a correct way to declare an abstract method?

a) abstract void myMethod() { }

b) void myMethod() { abstract; }

c) abstract void myMethod();

d) void abstract myMethod();

9. Which of the following statements about abstract classes is false?

a) Abstract classes can have constructors.

b) Abstract classes can implement interfaces.

c) Abstract classes can be instantiated using the new keyword.

d) Abstract classes can have non-abstract methods.

10. What happens if an abstract class contains only abstract methods?

a) It behaves exactly like an interface.

b) It cannot be extended by other classes.

c) It must be instantiated using an anonymous class.

d) It cannot contain any constructors.

This quiz ensures you have a solid grasp of abstract methods in Java, their use cases, and common mistakes developers often make.

Also Read: Top 8 Reasons Why Java Is So Popular and Widely Used in 2025

You can continue expanding your skills in Java with upGrad, which will help you deepen your understanding of advanced Java concepts and real-world applications.

upGrad’s courses offer expert training in Java programming, covering abstract methods, inheritance, and best practices for object-oriented design. Gain hands-on experience in implementing abstract methods, avoiding common pitfalls, and building efficient, maintainable Java applications.

Below are some relevant upGrad courses:

You can also get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today! 

Similar Reads:

FAQs

Q: Can an abstract class have a constructor?

A: Yes, an abstract class can have a constructor, which is called when a subclass object is instantiated.

Q: Can an abstract class extend a concrete class?

A: Yes, an abstract class can extend a concrete class and inherit its methods, but it must implement any abstract methods from its own hierarchy.

Q: Can an abstract method have parameters?

A: Yes, abstract methods can have parameters, just like regular methods, but they cannot have a method body.

Q: Is it possible to override a concrete method in an abstract class?A: Yes, an abstract class can have concrete methods, and subclasses can override them as needed.

Q: Can interfaces have abstract methods?

A: Yes, all methods in an interface are implicitly abstract unless they are declared default or static.

Q: What happens if a subclass provides a different signature for an inherited abstract method?

A: This results in a compilation error, as the method signature must exactly match the one in the abstract class.

Q: Can an abstract class implement an interface?

A: Yes, an abstract class can implement an interface, but it is not required to provide implementations for all interface methods.

Q: Can an abstract class have main() and be executed?

A: Yes, an abstract class can have a main() method and be executed like any other Java class, but it cannot be instantiated directly.

Q: Can we create an anonymous class from an abstract class?

A: Yes, an anonymous inner class can be created from an abstract class, and it must provide implementations for all abstract methods.

Q: What happens if an abstract class has a private method?

A: Private methods in an abstract class are allowed, but they cannot be inherited or overridden by subclasses.

Q: Can an abstract class be declared as final?

A: No, an abstract class cannot be final because final prevents further extension, which contradicts the purpose of abstraction.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.