Category: java
abstract class in Java
Published on 04 May 2026
Explanation
An abstract class in Java is a
class that cannot be instantiated and is
used as a base class for other
classes. It is declared using the
'abstract'
keyword and can contain both
abstract methods
(without implementation) and c
oncrete methods (with implementation).
Code:
abstract class Animal {
abstract void sound();
void eat() {
System.out.println("Animal
eats food");
}
}
Explanation
Abstract classes are mainly used to achieve
abstraction by hiding implementation
details and showing
only essential features to the user.
Code:
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Explanation
They help in code reusability by allowing
common methods to be defined once in
the abstract class and reused by multiple
subclasses.
Code:
abstract class Shape {
void display() {
System.out.println(
"This is a shape");
}
abstract void draw();
}
Explanation
Abstract classes provide a template
or blueprint
for other classes, ensuring that
subclasses implement
required methods.
Code:
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
Explanation
They support partial abstraction,
meaning some methods
can have implementation while others
are left
abstract for subclasses to define.
Code:
abstract class Vehicle {
abstract void start();
void stop() {
System.out.println("
Vehicle stopped");
}
}