In Java, abstract means incomplete.
It is used when a class or method is not fully finished and child classes must complete it.
Abstract Class
A class declared with abstract keyword is called an abstract class.
abstract class Animal {
}
- Cannot create object directly
- Mostly used as parent class
Animal a = new Animal(); // β Error
Abstract Method
An abstract method has:
- only declaration
- no body
abstract void sound();
Child class must implement it.
Example
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Here:
-
Animalgives only idea -
Dogcompletes the method
Simple Real-Life Example
Think about a βVehicleβ.
All vehicles move, but:
- Car moves differently
- Bike moves differently
So parent class says:
abstract void move();
Child classes define how they move.
Top comments (0)