What is Abstract Method in Java?
Learn via video course

What is Abstract Method in Java?
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 (a class that is declared using the abstract keyword).
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.
The basic syntax of the abstract method in java is:
Example
As we can see there is no definition of the method is present (the method is ending with a semicolon (;)).
Rules of Abstract Method
Before learning about the rules associated with the abstract method in java, let us first get a brief introduction to abstract classes and the need for abstract methods in java.
Sometimes in the case of inheritance, we only require specific method declaration in super-classes. The definition of these methods is defined in child class(es) according to the requirement. In such cases, we declare abstract methods in the abstract class and the child class(es) extending the parent abstract class must contain the definition of the abstract method. The abstract method in java is also known as subclasser responsibility because they have no implementation specified in the super-class.
Note:
- An abstract method is declared inside a class called abstract class, and the class that extends the abstract class is called concrete class.
- A concrete class must contain the definition of the inherited abstract method.
Abstraction means hiding irrelevant details from the user. Abstraction helps us to increase efficiency thereby reducing complexity. In java, we use abstract classes and abstract methods to achieve abstraction.
Let us now learn about the rules of 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 may or may not contain abstract methods in java.
- Abstract methods end with a semicolon rather than curly brackets or {}.
- If concrete or regular class extends an abstract class, then the child class must have to implement all the abstract methods of the parent abstract class. Else the child class has to be declared abstract as well.
Refer to the section below for examples.
Examples
So far we have learned a lot about abstract classes and abstract methods in java. Let us take some examples to understand the topic better.
A Java Program to Demonstrate the Use of Abstract Keyword
Suppose we are having an Animal class which is an abstract class containing abstract methods. The reason for declaring the Animal class as an abstract class is that every animal has different specifications like different legs, eyes, color, etc but all the 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 Animal class as per their requirement. The classes extending the Animal class will contain the definitions of abstract methods. Example
Output:
Abstract Method in Interface
As we know abstract methods are declared inside abstract classes but we can declare abstract methods inside an interface as well because all the methods of an interface are public abstract by default.
Note: What is an interface? An interface can have methods and variables just like the classes but the methods declared inside an interface are by default abstract.
Let us take an example to understand how abstract methods with interfaces in java.
Output:
Learn more
A class that is declared with the keyword abstract is known as an abstract class. An abstract class cannot be instantiated but can be only used as a superclass by its subclasses.
To learn more about abstract classes in java. Refer to: Abstract Classes in Java
Refer to the other useful Java Programming Topics such as:
Conclusion
- 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.
- The basic syntax of abstract method in java is: abstract returnType method-name(parameter-list);
- 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 may or may not contain abstract methods in java.
- Abstract methods ends with a semicolon rather than curly brackets or {}.
- If concrete or regular class extends an abstract class, then the child class must have to implement all the abstract methods of the parent abstract class. Else the child class has to be declared abstract as well.