Javatpoint Logo
Javatpoint Logo

Abstract Method in Java

In object oriented programming, abstraction is defined as hiding the unnecessary details (implementation) from the user and to focus on essential details (functionality). It increases the efficiency and thus reduces complexity.

In Java, abstraction can be achieved using abstract classes and methods. In this tutorial, we will learn about abstract methods and its use in Java.

Abstract class

A class is declared abstract using the abstract keyword. It can have zero or more abstract and non-abstract methods. We need to extend the abstract class and implement its methods. It cannot be instantiated.

Syntax for abstract class:

Note: An abstract class may or may not contain abstract methods.

Abstract Method

A method declared using the abstract keyword within an abstract class and does not have a definition (implementation) is called an abstract method.

When we need just the method declaration in a super class, it can be achieved by declaring the methods as abstracts.

Abstract method is also called subclass responsibility as it doesn't have the implementation in the super class. Therefore a subclass must override it to provide the method definition.

Syntax for abstract method:

Here, the abstract method doesn't have a method body. It may have zero or more arguments.

Points to Remember

Following points are the important rules for abstract method in Java:

  • An abstract method do not have a body (implementation), they just have a method signature (declaration). The class which extends the abstract class implements the abstract methods.
  • If a non-abstract (concrete) class extends an abstract class, then the class must implement all the abstract methods of that abstract class. If not the concrete class has to be declared as abstract as well.
  • As the abstract methods just have the signature, it needs to have semicolon (;) at the end.
  • Following are various illegal combinationsof other modifiers for methods with respect to abstract modifier:
    • final
    • abstract native
    • abstract synchronized
    • abstract static
    • abstract private
    • abstract strictfp
  • If a class contains abstract method it needs to be abstract and vice versa is not true.

Example of Abstract Method in Java

Example 1:

In the following example, we will learn how abstraction is achieved using abstract classes and abstract methods.


Abstract Method in Java

Example 2:

By default, all the methods of an interface are public and abstract. An interface cannot contain concrete methods i.e. regular methods with body.


Abstract Method in Java

In this way, we have learned about abstract method and its implementation in Java.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA