---
# System prepended metadata

title: Abstract Method in Java with Examples - Scaler Topics

---

---
title: Abstract Method in Java with Examples - Scaler Topics
description: Find out about Abstract Method in Java along with syntax, easy-to-grasp examples, and code explanations on Scaler Topics.
author: Sushant Gaurav
category: Java
---
:::section{.main}

Sometimes, in the case of inheritance, we only require specific method declaration in super-classes. These methods are defined in the child `class(es)` according to the requirement. In such cases, we declare abstract methods in the abstract class. A method that does not have its implementation or body is known as an abstract method in java. An [abstract method in java](https://www.javatpoint.com/abstract-method-in-java) is declared inside an abstract class. 

An [abstract class](https://www.scaler.com/topics/java/abstraction-in-java/) may or may not contain an `abstract method` in java. Abstract methods, on the other hand, must be declared inside an abstract class and do not have a default implementation. Subclasses that extend an abstract class must either provide an implementation for all of its abstract methods or be declared as abstract themselves.

:::

:::section{.main}

## Java Abstract Method

### Syntax for Abstract Method in Java

The basic **syntax** of the abstract method in java is:
```java
abstract returnType methodName(parameterList);
```
**Example**
```java
public abstract void calculate(int x, int y);
```
As we can see, no definition of the method is present (the method ends with a **semicolon** (**;**)).

### Important Points for Java Abstract Method

The abstract method in Java is also known as **subclasses responsibility** because they have no implementation specified in the super-class. There are some essential points for abstract method in java, which are listed below as-
1. An abstract method is declared inside an abstract class, and the class that extends the abstract class is called the **concrete** class.
2. A concrete class must contain the definition of the inherited abstract method.
3. If a class has an abstract method, then the class should be declared abstract as well.
4. Abstract methods in Java are not implemented; they only have a method signature (declaration).
5. An abstract class may or may not contain abstract methods in java.
6. Abstract methods end with a semicolon rather than curly brackets or **{}**.
7. If a concrete or regular class extends an abstract class, then the child class must implement all the abstract methods of the parent abstract class. Otherwise, the child class must be declared abstract as well.
   
:::

:::section{.main}
## Example of Abstract Method in Java 

```java
abstract class Animal {
    String Name = " ";

    Animal(String name) {
        this.Name = name;
    }

    //We can declare non-abstract methods in the abstract class.
    public void Information(String detail) {
        System.out.println("The details of " + this.Name + " is: " + detail);
    }

    // declaring the signatures of abstract methods
    abstract public void legs();
    abstract public void eyes();
}

class Camel extends Animal {

    // constructor
    Camel(String name) {
        super(name);
    }

    // Overriding or defining the abstract methods:
    @Override
    public void legs() {
        System.out.println("It has 4 legs.");
    }

    @Override
    public void eyes() {
        System.out.println("It has 2 eyes.");
    }
}

class Pigeon extends Animal {

    // constructor
    Pigeon(String name) {
        super(name);
    }

    // Overriding or defining the abstract methods:
    @Override
    public void legs() {
        System.out.println("It has 2 legs.");
    }

    @Override
    public void eyes() {
        System.out.println("It has 2 eyes.");
    }
}


class Ant extends Animal {

    // constructor
    Ant(String name) {
        super(name);
    }

    // Overriding or defining the abstract methods:
    @Override
    public void legs() {
        System.out.println("It has 6 legs.");
    }

    @Override
    public void eyes() {
        System.out.println("It has 2 eyes.");
    }
}


public class Test {
    public static void main(String[] args) {

        //creates the object of child classes and uses animal class reference.
        Animal object1 = new Camel("Camel");
        object1.Information("Camels are found in desert.");
        object1.legs();
        object1.eyes();

        System.out.println();

        Animal object2 = new Pigeon("Pigeon");
        object2.Information("Pigeons are intelligent animals.");
        object2.legs();
        object2.eyes();

        System.out.println();

        Animal object3 = new Ant("Ant");
        object3.Information("there are over 12,000 ant species worldwide.");
        object3.legs();
        object3.eyes();
    }
}
```

**Output:**
```plaintext
The details of Camel is: Camels are found in the desert.
It has 4 legs.
It has 2 eyes.

The details of Pigeon is: Pigeons are intelligent animals.
It has 2 legs.
It has 2 eyes.

The details of Ant are: there are over 12,000 ant species worldwide.
It has 6 legs.
It has 2 eyes.
```
**Explanation:** In the above example, we have an `Animal` class, which is an abstract class containing abstract methods. Every animal has different specifications, like different legs, eyes, and color, but all animals belong to a single kingdom, so we can declare an Animal class as an abstract class. The child classes like Monkey, Camel, Lion, etc. can declare the abstract methods of the Animal class as per their requirements. 
:::

:::section{.main}

## Abstract Method in Interface

As we know, abstract methods are declared inside abstract classes, but we can also declare abstract methods inside an interface because all interface methods are public abstract by default.

Let us take an **example** to understand how abstract methods work with interfaces in Java.

```java
// declaring an interface
interface Sum {
    // declaring abstract methods inside the interface
    public abstract int SumTwo(int a, int b);

    //By default, the methods are public abstracts inside an interface.
    // so there is no need to mention the public abstract before the return type.
    int SumThree(int a, int b, int c);
}

public class Test implements Sum {
    public int SumTwo(int a, int b) {
        return a + b;
    }

    public int SumThree(int a, int b, int c) {
        return a + b + c;
    }

    public static void main(String args[]) {
        Sum object = new Test();
        System.out.println(object.SumTwo(33, 45));
        System.out.println(object.SumThree(11, 89, 20));
    }
}
```
**Output:**
```plaintext
78
120
```
:::

:::section{.summary}
## Conclusion
- A method without implementation or body is known as an abstract method in java. 
- If a class has an abstract method, then the class should be declared abstract as well.
- Abstract methods in Java do not have an implementation; abstract methods only have a method signature (declaration).
- An abstract class in java may or may not contain abstract methods.
- Abstract methods ends with a **semicolon** rather than curly brackets or **{}**.

:::
