For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
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!
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:
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:
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
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:
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.
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:
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:
Also Read: OOPS Concept in Java Explained for Beginners
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
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:
Code Example:
class Car extends Vehicle {
// Implementing the abstract method
@Override
void startEngine() {
System.out.println("Car engine started.");
}
}
In this example:
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:
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.
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 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:
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:
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.
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
Abstract methods are essential when designing a base class that defines a common interface for all its subclasses. They should be used when:
While abstraction can enhance code organization, excessive abstraction can lead to unnecessary complexity. To avoid this:
When defining an abstract method:
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.
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:
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.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
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.