Abstract Method in Java with Examples

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

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 is declared inside an abstract class.

An abstract class 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.

Java Abstract Method

Syntax for Abstract Method in Java

The basic syntax of the abstract method in Java is:

Example

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.

Example of Abstract Method in Java

Output:

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.

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.

Output:

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 {}.