Open In App

Abstract Method in Java with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In Java, Sometimes we require just method declaration in super-classes. This can be achieved by specifying the Java abstract type modifier. Abstraction can be achieved using abstract class and abstract methods. In this article, we will learn about Java Abstract Method.

Java Abstract Method

The abstract Method is used for creating blueprints for classes or interfaces. Here methods are defined but these methods don’t provide the implementation. Abstract Methods can only be implemented using subclasses or classes that implement the interfaces.

These methods are sometimes referred to as subclasser responsibility because they have no implementation specified in the super-class. Thus, a subclass must override them to provide a method definition. 

Declare Abstract Method in Java

To declare an abstract method, use this general form:

abstract type method-name(parameter-list);

As you can see, no method body is present. Any concrete class(i.e. class without abstract keyword) that extends an abstract class must override all the abstract methods of the class.

Important Points for Abstract Method

Important rules for abstract methods are mentioned below:

  • Any class that contains one or more abstract methods must also be declared abstract.
  • If a class contains an abstract method it needs to be abstract and vice versa is not true.
  • If a non-abstract class extends an abstract class, then the class must implement all the abstract methods of the abstract class else the concrete class has to be declared as abstract as well.
  • The following are various illegal combinations of other modifiers for methods with respect to abstract modifiers: 
    • final
    • abstract native
    • abstract synchronized
    • abstract static
    • abstract private
    • abstract strictfp

Example of Java Abstract Method

Example 1:   Write a program To display the method print the addition and subtraction by using abstraction.

Java




// Java Program to implement To display the
// method print the addition and subtraction
// by using abstraction
 
// Abstract Class
abstract class arithmetic_operation {
    abstract void printInfo();
}
 
// Class add
class add extends arithmetic_operation {
    // class add must override printInfo() method
    // otherwise, compile-time
    // exception will be thrown
    void printInfo()
    {
        int a = 3;
        int b = 4;
        System.out.println(a + b);
    }
}
 
// Class sub
class sub extends arithmetic_operation {
    // class sub must override printInfo() method
    // otherwise, compile-time
    // exception will be thrown
    void printInfo()
    {
        int c = 4;
        int d = 5;
        System.out.println(c - d);
    }
}
 
// Driver Class
class abstraction {
    // Main Function
    public static void main(String args[])
    {
        arithmetic_operation n = new add();
        n.printInfo();
        arithmetic_operation y = new sub();
        y.printInfo();
    }
}


Output

7
-1

Example 2: Consider the following Java program, that illustrates the use of abstract keywords with classes and methods. 

Java




// A Java program to demonstrate
// use of abstract keyword.
 
// abstract class
abstract class A {
    // abstract method
    // it has no body
    abstract void m1();
 
    // concrete methods are still
    // allowed in abstract classes
    void m2()
    {
        System.out.println("This is "
                           + "a concrete method.");
    }
}
 
// concrete class B
class B extends A {
    // class B must override m1() method
    // otherwise, compile-time
    // exception will be thrown
    void m1()
    {
        System.out.println("B's "
                           + "implementation of m1.");
    }
}
 
// Driver class
public class AbstractDemo {
    // main function
    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: Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java’s approach to run-time polymorphism is implemented through the use of super-class references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object.

Example 3: Abstract class with an abstract method.

Java




// Java Program to implement
// Abstract class with 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.

Below is the implementation of the above method:

Java




// Java Program to implement
// Abstract Method in Interface
import java.io.*;
 
// declaring an interface
interface Sum {
    // declaring abstract methods inside the interface
    public abstract int Operation_two_var(int a, int b);
 
    int Operation_three_var(int a, int b, int c);
}
 
// Main Class
public class GFG implements Sum {
    public int Operation_two_var(int a, int b)
    {
        return a * b;
    }
 
    public int Operation_three_var(int a, int b, int c)
    {
        return a * b * c;
    }
 
    // main function
    public static void main(String args[])
    {
        Sum object = new GFG();
        System.out.println(
            object.Operation_two_var(10, 20));
        System.out.println(
            object.Operation_three_var(10, 20, 30));
    }
}


Output

200
6000


Last Updated : 23 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads