what is abstract method in java
An abstract method in Java is a method that is declared without any implementation (no method body) and is meant to be overridden by subclasses to provide actual behavior.
What is an abstract method in Java?
In Java, an abstract method:
- Is declared with the
abstractkeyword and ends with a semicolon instead of a method body.
- Provides only a signature (return type, name, parameters) and no implementation.
- Must be implemented (overridden) by concrete subclasses; otherwise those subclasses must also be declared abstract.
Simple definition:
An abstract method is a method that has no body and acts as a contract that forces subclasses to provide their own implementation.
Basic syntax (quick view)
java
abstract class Animal {
abstract void makeSound(); // abstract method (no body)
}
Key points from this example:
abstractappears before the return type (void).
makeSound();ends with;and has no{ }body.
- The class
Animalmust be abstract if it contains an abstract method.
Where can abstract methods live?
Abstract methods:
- Can only exist inside abstract classes or interfaces.
- If a class has at least one abstract method, that class must be declared abstract.
- An interface’s methods are implicitly abstract (in older Java versions; now they can also be default or static), but the core idea is the same: no body, must be implemented by implementing classes.
Why use abstract methods? (Real-world intuition)
Think of an abstract method as a rule in a base blueprint that says “every subclass must define this behavior in its own way.”
Example idea:
- Abstract class:
Vehiclewith abstract methodmove().Carimplementsmove()using wheels.Boatimplementsmove()using water propulsion.
The abstract method guarantees that every Vehicle subclass has a
move() method, even though the details differ.
Rules and constraints (important points)
Some core rules about abstract methods in Java:
- No body allowed
- Declaration like
public abstract void doWork();is valid.
- Declaration like
* `public abstract void doWork() { }` is invalid because an abstract method cannot have a body.
- Class must be abstract
- If a class contains any abstract method, the class must also be declared abstract.
- Subclasses must implement them
- A non-abstract (concrete) subclass extending an abstract class must implement all inherited abstract methods; otherwise it must itself be declared abstract.
- Cannot combine with some modifiers
- Abstract methods cannot be
finalorstatic, because they are meant to be overridden and implemented in subclasses.
- Abstract methods cannot be
Quick example with implementation
java
abstract class Main {
public String name = "John";
public int age = 24;
public abstract void study(); // abstract method
}
class Student extends Main {
public int graduationYear = 2018;
@Override
public void study() { // implementation of abstract method
System.out.println("Studying all day long");
}
}
Here:
study()inMainis abstract and has no body.
Studentprovides the actual body forstudy().
- You cannot create
new Main(), but you can createnew Student()and callstudy()on it.
Mini FAQ style recap
- Q: What is an abstract method in Java?
A method declared withabstractand no body, forcing subclasses to provide the implementation.
- Q: Can we instantiate a class with an abstract method?
No, such a class must be abstract and cannot be instantiated directly.
- Q: Do all subclasses have to implement abstract methods?
Yes, unless the subclass is also declared abstract.
TL;DR:
An abstract method in Java is a method declared with the abstract keyword
and no body, defined in an abstract class or interface, and it acts as a
contract that all concrete subclasses must implement.
Information gathered from public forums or data available on the internet and portrayed here.