Java Abstract Classes Tutorial
Abstract classes in Java allows us to create a blueprint for a class but hide the implementation details of some methods. It is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
What is an Abstract Class?
A class which is declared with the abstract keyword is known as an abstract class in Java.
It can have abstract and non-abstract methods (method with the body).
Key Points:
- An abstract class must be declared with an abstract keyword.
- It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have constructors and static methods also.
- It can have final methods which will force the subclass not to change the body of the method.
Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as an abstract method.
abstract void printStatus(); //no method body and abstract Example of Abstract Class
abstract class Bike {
abstract void run();
}
class Honda4 extends Bike {
void run() {
System.out.println("running safely");
}
public static void main(String args[]) {
Bike obj = new Honda4();
obj.run();
}
}