The body of abstract methods declared in an interface
will be defined in the class that implements the interface.
Understanding Interface:
An interface in Java is used to achieve:
- Abstraction
- Multiple inheritance
- Standardized behavior
Methods inside an interface are:
abstract and public by default
Interface Methods Do Not Have Body:
interface Shape
{
void draw();
}
Here:
draw() is an abstract method
- It contains only declaration
- No method body is present
Where is the Body Defined?
The body of the abstract method is defined inside the
class that implements the interface.
class Circle implements Shape
{
public void draw()
{
System.out.println("Drawing Circle");
}
}
Step-by-Step Explanation:
| Component |
Purpose |
| Interface |
Contains abstract method declaration |
| Implementing Class |
Provides method body (implementation) |
Important Rule:
If a class implements an interface,
it must provide implementation of all abstract methods.
What Happens if Method Body is Not Defined?
If the implementing class does not define the method body,
Java produces:
Compile-Time Error
Example:
interface Test
{
void show();
}
class Demo implements Test
{
}
Error occurs because:
show() method is not implemented
Important Concept:
Interface → Declaration only
Implementing Class → Definition/Body
Final Conclusion:
-
Interface methods are abstract by default.
-
Their method bodies are written inside the implementing class.
-
The implementing class must override all abstract methods.