Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Abstraction in java → Abstract method

Abstraction in java

Abstract method

Abstract Method Declaration

An abstract method in Java is declared using the abstract keyword. Here’s the syntax for declaring an abstract method:
Abstract method basic syntaxabstract return_type method_name ( [ argument-list ] );
As you can see, no method body is present. The abstract method just has a method signature (declaration).

Abstract Method Definition

The definition (implementation) of an abstract method is provided by the subclass that extends the abstract class. If a non-abstract (concrete) class extends an abstract class, then the class must implement all the abstract methods of the abstract class. Example of Abstract Method in Java Here’s an example of an abstract method in Java:
Abstract method basic example in java // abstract class abstract class Animal { // abstract method public abstract void animalSound(); } // subclass (inherit from Animal) class Pig extends Animal { public void animalSound() { // The body of animalSound() is provided here System.out.println("The pig says: wee wee"); } } class Main { public static void main(String[] args) { Pig myPig = new Pig(); // Create a Pig object myPig.animalSound(); } }


The pig says: wee wee
In this example, Animal is an abstract class that has an abstract method animalSound(). The class Pig extends Animal and provides the implementation for the animalSound() method.

Important Points about Abstract Methods

✦ An abstract method does not have a body (implementation), they just have a method signature (declaration). ✦ If a non-abstract (concrete) class extends an abstract class, then the class must implement all the abstract methods of that abstract class. ✦ 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. ✦ If a class contains an abstract method it needs to be abstract and vice versa is not true. Remember, abstract methods are a crucial part of the Java Object-Oriented Programming (OOP) concepts, allowing for abstraction and encapsulation.