Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
AbstractMap put() Method in Java with Examples
Next article icon

Abstract Method in Java with Examples

Last Updated : 26 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
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 subclass 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 


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.

Java
import java.io.*;  // Abstract class with abstract methods abstract class Geometry {     // declaring abstract methods     abstract void rectangle_area(int height, int width);     abstract void square_area(int side);     abstract void circle_area(float radius);      // declaring a final method     final void rectangle_perimeter(int height, int width) {         int perimeter = 2 * (height + width);         System.out.println("Perimeter of rectangle=" + perimeter);     } }  // 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 implementations         obj.rectangle_area(12, 13);         obj.square_area(12);         obj.circle_area(2.2f);          // calling the final method         obj.rectangle_perimeter(12, 13);     } } 

Output

Area of rectangle=156
Area of square=144
Area of circle=15.1976
Perimeter of rectangle=50




Next Article
AbstractMap put() Method in Java with Examples

M

monster5
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2019
Practice Tags :
  • Java

Similar Reads

  • AbstractMap put() Method in Java with Examples
    The AbstractMap put() method inserts a mapping into an AbstractMap. This means we can add a specific key and its value, into a particular map. If an existing key is passed, then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Exampl
    3 min read
  • AbstractMap get() Method in Java with Examples
    The AbstractMap.get() method of AbstractMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. Syntax: AbstractMap.get(Object key_element) Parameter: The method takes one parameter key
    2 min read
  • AbstractList get() method in Java with Examples
    The get() method of java.util.AbstractList class is used to return the element at the specified position in this list. Syntax: public abstract E get(int index) Parameters: This method takes index of the element as a parameter, the element at which is to be returned. Returns Value: This method return
    2 min read
  • AbstractList set() Method in Java with Examples
    The set() method of java.util.AbstractList class is used to replace any particular element in the abstract list created using the AbstractList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() metho
    2 min read
  • AbstractMap clear() Method in Java with Examples
    The AbstractMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified Map. Syntax: AbstractMap.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the wo
    2 min read
  • AbstractSet size() method in Java with Example
    The size() method of AbstractSet in Java is used to get the size for this instance of the AbstractSet. It returns an integer value which is the size for this instance of the AbstractSet. Syntax: public int size() Parameters: This function has no parameters. Returns: The method returns an integer val
    2 min read
  • AbstractMap values() Method in Java with Examples
    The AbstractMap.values() method of AbstractMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the AbstractMap. Syntax: AbstractMap.values() Parameters: The method does not accept any parameters. Return Value: The met
    2 min read
  • AbstractMap putAll() Method in Java with Examples
    The AbstractMap.putAll() is an inbuilt method of AbstractMap class that is used for the copy operation. The method copies all of the elements i.e., the mappings, from one map into another. Syntax: new_abstract_map.putAll(exist_abstract_map) Parameters: The method takes one parameter exist_abstract_m
    2 min read
  • AbstractMap keySet() Method in Java with Examples
    The AbstractMap.keySet() method in Java is used to create a set out of the key elements contained in the abstract map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: AbstractMap.keySet() Parameters: The method does not take any para
    2 min read
  • AbstractMap remove() Method in Java with Examples
    The AbstractMap.remove() is an inbuilt method of AbstractMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Syntax: AbstractMap.remove(Object key) Parameters: The method takes one parameter key whose mapp
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences