Abstract method in java is a method which does not have an implementation or a body where it is declared.
It is defined in a child(or sub) class.
Declaration includes its access specifier, return type, method name and arguments followed by a semi-colon.
An abstract method is declared in an abstract class or in an interface.
In this article, you will learn about java abstract methods in detail.

Syntax
As stated above, an abstract method can only be declared. Its declaration should include following

  • abstract keyword
  • Access specifier
  • Return type
  • Method name
  • Arguments(optional)
  • throws declaration(optional), if the method implementation is required to throw exceptions

Example of abstract method declaration is given below.

public abstract makeSound();

Remember that abstract method declaration is followed by a semi-colon instead of curly braces as in a normal java method.
Semi-colon instructs the compiler that this method has only been declared here, it will be implemented somewhere else.

Implementation
An abstract method is not implemented where it is declared, its implementation(or definition) is the responsibility of

  1. a sub-class(if abstract method is declared in a class), or
  2. a class that implements an interface(if abstract method is declared in an interface).

If an abstract method is declared in a class, then the class should be an abstract class, that is, class should be declared with abstract keyword.
Methods declared in an interface are implicitly abstract, even if their declaration does not contain abstract keyword and unless they are default interface methods or static interface methods.
Default and static interface methods in interface were introduced in java 8.
When a sub-class implements or defines an abstract method of its parent(or base) class or an interface, then it is also called method overriding.
Example
Following is an example of abstract method declaration and its implementation in a java class.

public abstract class XMLRequestSender {

   public abstract sendRequest();

}

Above code snippet defines a class with an abstract method to send a request. This method is implemented by its child class as follows.

public class XMLOverHTTPRequestSender extends XMLRequestSender {

   // implement abstract method
   @Override
   public sendRequest() {
      System.out.print("Sending XML over HTTP request");
   }
}

Another child class with its own implementation of abstract method is as follows.

public class SOAPXMLRequestSender extends XMLRequestSender {
   
   // implement abstract method
   @Override
   public sendRequest() {
      System.out.print("Sending XML SOAP request");
   }
}

Note that when child classes implement abstract method, it contains a body.
Also, implemented method is annotated with @Override annotation since they are implementing a method declared in parent class.
Abstract method implementation can be tested using the following class containing main method.

public class AbstractMethodDemo {
   public static void main(String... a) {
      // object of child class
      XMLRequestSender xmlOverHttp = new XMLOverHTTPRequestSender();
      xmlOverHttp.sendRequest();
       // object of second child class
      XMLRequestSender xmlSoap = new SOAPXMLRequestSender();
      xmlSoap.sendRequest();
   }
}

Above code prints

Sending XML over HTTP request
Sending XML SOAP request

Why abstract methods
Abstract methods are declared when its implementation is not known to the declaring class or its functionality depends on the child class.
In the above example, parent class does not know how to execute a request.
Both its child classes implement the abstract method with their own different request sending logic.
Another real life example of an abstract method could be an operating system parent class with an abstract method defineLookAndFeel and each of its child class overriding it to define its own look and feel.
More about abstract methods
Below is a list of questions that arise when learning abstract methods and are also frequently asked in an interview.
1. Can abstract method be final
The answer is No.
A method can not be abstract and final at the same time.
An abstract method needs to be implemented and final methods can not be implemented.
Both contradict each other.
Compiler knows it and hence will flag an error.
2. Can abstract method be private?
Again, No.
Private methods are not visible outside a class, not even to the child class. If an abstract method is not visible to the child class, then how will it be defined.
3. If a class does not contain an abstract method, can it be declared abstract?
Yes.
A class can be declared abstract even if it does not contain any abstract method but if it contains a single abstract method then the class should be abstract.
4. Can abstract method be static?
An abstract method can not be static.
An abstract method must be implemented or overridden in a child class but static methods can not be overridden.
5. What if child class does not implement abstract method of parent class?
In this case, the child class should also be made abstract.
All the classes in the hierarchy will be abstract till the one which implements abstract method.
6. Can interface method be abstract and default?
No. Since default interface method is supposed to have a body while abstract method can not.
Illegal combinations with abstract
abstract keyword can not be combined with below keywords.

  • final
  • synchronized
  • strictfp
  • static
  • native
  • private

That is all on abstract methods in java. Hit the clap if the article was useful.

Leave a Reply