The abstract keyword in Java is used to define:
- Abstract classes
- Abstract methods
It allows you to create a base structure without specifying full implementation. Abstraction focuses on what needs to be done, not how it will be done.
An abstract method:
- Has a declaration (signature)
- Has no body
- Must be implemented by child classes
abstract void draw();abstract class Shape {
abstract void draw(); // no implementation
}Child class must implement it:
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}| Rule | Explanation |
|---|---|
| Must be inside an abstract class | Cannot exist in a normal class |
| Cannot be private | Must be overridden by child |
| Cannot be final | Final methods cannot be overridden |
| Cannot be static | Abstract behavior belongs to instance, not class |
| Must be overridden in child class | Unless child is abstract |
A class declared with abstract keyword cannot be instantiated.
abstract class Animal {
abstract void sound(); // abstract method
void sleep() { // normal method
System.out.println("Animal sleeps");
}
}- Can contain abstract + concrete methods
- Can have constructors
- Can have variables, static methods, blocks
- Can extend another class (abstract or concrete)
Animal a = new Animal(); // error: cannot instantiate abstract classEven though abstract classes cannot be instantiated, they can have constructors. These are used for initial setup of fields.
abstract class Person {
String name;
Person(String name) {
this.name = name;
}
abstract void work();
}
class Employee extends Person {
Employee(String name) {
super(name);
}
void work() {
System.out.println(name + " is working");
}
}Abstract class provides partial abstraction.
- Some methods can be abstract
- Some methods can be fully implemented
This is different from interfaces (which originally gave 100% abstraction).
- You want to provide common functionality in base class
- You want child classes to implement specific behavior
- You want to avoid object creation of incomplete classes
A class like Shape is too generic to create objects:
Shape s = new Shape(); // meaninglessBut: Circle, Rectangle, Triangle → meaningful.
abstract class Payment {
abstract void pay(double amount);
void showReceipt() {
System.out.println("Payment completed");
}
}
class UPI extends Payment {
void pay(double amount) {
System.out.println("Paid via UPI: " + amount);
}
}Usage:
Payment p = new UPI();
p.pay(400);
p.showReceipt();| Feature | abstract class | interface |
|---|---|---|
| Methods | Abstract + concrete | Abstract, default, static |
| Variables | Any type | public static final only |
| Constructor | Allowed | Not allowed |
| Multiple inheritance | Only one abstract class | Multiple interfaces |
| Abstraction level | Partial | Complete (mostly) |
| Combination | Valid? | Reason |
|---|---|---|
| abstract + final method | ❌ | final cannot be overridden |
| abstract + static method | ❌ | static cannot be abstract |
| abstract + private method | ❌ | private cannot be overridden |
| abstract class with final methods | ✔ | final concrete methods allowed |
| abstract class with static methods | ✔ | static concrete methods allowed |
When creating a child object:
Payment p = new UPI();Memory:
Reference type: Payment (abstract)
Object type: UPI (concrete)
- Abstract methods resolved via overriding
- Concrete methods inherited normally
Yes. Used to initialize common fields.
No. It must be accessible to child classes for overriding.
Yes, unless the child class is also abstract.
To provide a base template with some enforced behavior.
No. It contradicts the purpose of inheritance.
-
abstractkeyword is used for classes and methods. -
Abstract methods have no body and must be implemented by child classes.
-
Abstract classes cannot be instantiated, but can have constructors, fields, and methods.
-
Provides partial abstraction and supports inheritance.
-
Used to enforce a structure while allowing flexible implementations.