An abstract method in Java is a method that is declared without a body and is marked using the abstract keyword. It specifies what a method should do but leaves the implementation to subclasses. Abstract methods are used to achieve abstraction by defining a common contract that derived classes must follow.
- Must be implemented by subclasses.
- Helps achieve abstraction in Java.
- Supports runtime polymorphism.
// Abstract class
abstract class ArithmeticOperation{
abstract void printInfo();
}
// Addition class
class Add extends ArithmeticOperation{
void printInfo() {
int a = 3, b = 4;
System.out.println(a + b);
}
}
// Subtraction class
class Sub extends ArithmeticOperation{
void printInfo() {
int a = 4, b = 5;
System.out.println(a - b);
}
}
// Driver class
class GFG{
public static void main(String[] args) {
ArithmeticOperation op1 = new Add();
op1.printInfo();
ArithmeticOperation op2 = new Sub();
op2.printInfo();
}
}
Output
7 -1
Explanation: In this example, the abstract class ArithmeticOperation declares an abstract method printInfo(). The Add and Sub classes extend the abstract class and provide their own implementations of printInfo(). When the method is called using the abstract class reference, the corresponding subclass implementation executes, demonstrating runtime polymorphism.
Syntax
abstract returnType methodName(parameterList);
- abstract + returnType: Defines a method with no body and specifies what type of value it will return (e.g., int, void, String)
- methodName: The name used to identify and call the method
- parameterList: Inputs given to the method inside () (can be empty, single, or multiple values)
Example of Java Abstract Method
Example 1: Abstract Class with Abstract and Concrete Methods
abstract class A{
abstract void m1();
void m2() {
System.out.println("This is a concrete method.");
}
}
class B extends A {
void m1() {
System.out.println("Implementation of abstract method m1.");
}
}
public class GFG{
public static void main(String[] args) {
B b = new B();
b.m1();
b.m2();
}
}
Output
B's implementation of m1. This is a concrete method.
Explanation: In this example, class A contains both an abstract method m1() and a concrete method m2(). Class B extends A and implements the abstract method m1(). The object of class B can access both the implemented abstract method and the inherited concrete method.
Note: Abstract classes cannot be instantiated, but their references can point to subclass objects, enabling runtime polymorphism.
Example 2: Abstract class with an abstract method.
import java.io.*;
abstract class Geometry {
// declaring abstract method
abstract void rectangle_area(int height, int width);
abstract void square_area(int side);
abstract void circle_area(float radius);
}
// extending abstract class
class Easy extends Geometry {
// implementing abstract method of abstract class
public void rectangle_area(int height, int width)
{
int ar = height * width;
System.out.println("Area of rectangle=" + ar);
}
// implementing abstract method of abstract class
public void square_area(int side)
{
int ar = side * side;
System.out.println("Area of square=" + ar);
}
// implementing abstract method of abstract class
public void circle_area(float radius)
{
float ar = 3.14f * radius * radius;
System.out.println("Area of circle=" + ar);
}
// main function
public static void main(String[] args)
{
// creating instance of derived class
Easy obj = new Easy();
// calling abstract method
obj.rectangle_area(12, 13);
obj.square_area(12);
obj.circle_area(2.2f);
}
}
Output
Area of rectangle=156 Area of square=144 Area of circle=15.197601
Explanation: In this example, the abstract class Geometry defines three abstract methods for calculating rectangle, square, and circle areas. The Easy class implements all these methods and provides the actual area calculation logic. This ensures a common structure while allowing specific implementations.
Java Abstract Method in Interface
All the methods of an interface are public abstract by default because of which we can declare abstract methods inside an interface.
interface Multiply {
int twoVar(int a, int b);
int threeVar(int a, int b, int c);
}
class Demo implements Multiply {
public int twoVar(int a, int b) {
return a * b;
}
public int threeVar(int a, int b, int c) {
return a * b * c;
}
public static void main(String[] args) {
Multiply m = new Demo();
System.out.println(m.twoVar(10, 20));
System.out.println(m.threeVar(10, 20, 30));
}
}
Output
200 6000
Explanation: In this example, the interface Multiply declares abstract methods for multiplication operations. The Demo class implements the interface and provides method implementations. This demonstrates how interfaces use abstract methods to define behavior contracts.
Final Method in Abstract Class
As we have mentioned that we cannot use final with abstract Method as a modifier but we can create a Final method in abstract class.
abstract class Geometry {
abstract void rectangleArea(int h, int w);
final void rectanglePerimeter(int h, int w) {
System.out.println("Perimeter = " + 2 * (h + w));
}
}
class Easy extends Geometry {
void rectangleArea(int h, int w) {
System.out.println("Area = " + (h * w));
}
public static void main(String[] args) {
Easy e = new Easy();
e.rectangleArea(12, 13);
e.rectanglePerimeter(12, 13);
}
}
Output
Area = 156 Perimeter = 50
Explanation: In this example, the abstract class Geometry contains an abstract method rectangleArea() and a final concrete method rectanglePerimeter(). The abstract method must be implemented by subclasses, while the final method cannot be overridden, ensuring consistent behavior.