An abstract method is a method declared without an implementation (i.e., a body). It only defines the method signature, and subclasses must provide the implementation.
- Abstract methods declared using the abstract keyword
- A class containing one or more abstract methods must be declared abstract.
- Does not have a body
- Can exist only inside an abstract class or interface
- Subclasses must override and implement it
- Abstract methods cannot use the following modifiers(final, static, private, synchronized, native, strictfp).
Example 1: Abstract Method for Addition and Subtraction
// 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
Syntax
abstract returnType methodName(parameterList);
Example:
abstract void display();
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.
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
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
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