An abstract method is a method whose implementation is deferred to a subclass.
In Core Java, an abstract method is a method that is declared without providing an implementation in the class where it is declared. It is meant to be overridden by subclasses that extend the class containing the abstract method. The abstract method is marked with the abstract
keyword.
Here’s an example:
abstract class Shape {
// Abstract method
abstract void draw();
}
class Circle extends Shape {
// Implementation of the abstract method in the subclass
void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
// Implementation of the abstract method in the subclass
void draw() {
System.out.println("Drawing a square");
}
}
In this example, the Shape
class has an abstract method draw()
. The subclasses Circle
and Square
provide their own implementations for the draw()
method. Any class that extends an abstract class with abstract methods must either provide implementations for all the abstract methods or be declared abstract itself.