Abstract Class
Java Abstract Classes and Methods
1. Abstract Class
An abstract class is a class declared with abstract.
-
Cannot create an object directly.
-
Can be extended by other classes.
-
Can contain both abstract and normal methods.
-
Can have fields, constructors, static methods, etc.
abstract class Animal {
String name;
abstract void sound();
void eat() {
System.out.println("Eating");
}
}
You cannot do:
Animal a = new Animal(); // ERROR
But you can:
class Dog extends Animal {
@Override
void sound() {
System.out.println("Woof");
}
}
Dog dog = new Dog();
dog.eat();
dog.sound();
2. Abstract Method
An abstract method has no body.
abstract void sound();
It basically says:
"Every concrete subclass must provide its own implementation."
Example:
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Woof");
}
}
3. If Subclass Doesn't Implement It
If a subclass doesn't implement all abstract methods, it must also be abstract.
abstract class Animal {
abstract void sound();
abstract void move();
}
abstract class Dog extends Animal {
void sound() {
System.out.println("Woof");
}
// move() not implemented
}
A concrete subclass must implement the remaining methods:
class Puppy extends Dog {
void move() {
System.out.println("Running");
}
}
4. Abstract Class Can Have Normal Methods
This is one of the main benefits.
abstract class Animal {
abstract void sound();
void eat() {
System.out.println("Eating");
}
}
Subclasses must implement sound(), but they automatically get eat().
So:
Abstract method → subclass must implement
Normal method → subclass can directly use
5. Abstract Class Can Have Fields
Unlike interfaces, abstract classes can have normal instance variables.
abstract class Animal {
String name;
int age;
abstract void sound();
void showName() {
System.out.println(name);
}
}
The fields can be changed:
Dog dog = new Dog();
dog.name = "Bruno";
dog.age = 5;
6. Abstract Class Can Have Constructors
abstract class Animal {
String name;
Animal(String name) {
this.name = name;
}
abstract void sound();
}
The subclass calls the constructor:
class Dog extends Animal {
Dog(String name) {
super(name);
}
void sound() {
System.out.println("Woof");
}
}
Dog dog = new Dog("Bruno");
Even though you cannot create an Animal object directly, its constructor is called when creating a Dog.
7. Abstract Class vs Interface
| Abstract Class | Interface | |
|---|---|---|
| Object creation | ❌ | ❌ |
| Instance fields | ✅ | ❌ |
| Constructors | ✅ | ❌ |
| Abstract methods | ✅ | ✅ |
| Normal methods | ✅ | default/static/private methods |
| Static fields | ✅ | Always public static final |
| Access modifiers | Any | Mostly public; private methods allowed |
| Extend/implement | One class | Multiple interfaces |
| Main purpose | Shared state + behavior | Contract/behavior |
8. When to Use Abstract Class?
Use an abstract class when classes are closely related and need to share state and code.
Example:
abstract class Vehicle {
int speed;
void start() {
System.out.println("Vehicle started");
}
abstract void move();
}
Then:
class Car extends Vehicle {
void move() {
System.out.println("Car is moving");
}
}
class Bike extends Vehicle {
void move() {
System.out.println("Bike is moving");
}
}
Both Car and Bike share:
speed
start()
but provide their own:
move()
9. Abstract Class Implementing an Interface
An abstract class does not have to implement every interface method.
interface Animal {
void sound();
void move();
}
abstract class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
// move() not implemented
}
Dog must be abstract because move() is still missing.
A concrete subclass can finish it:
class Puppy extends Dog {
public void move() {
System.out.println("Running");
}
}
10. Abstract Classes Can Have Static Members
abstract class Animal {
static int count = 0;
static void showCount() {
System.out.println(count);
}
}
You can call:
Animal.showCount();
You don't need an object.
Quick Revision
| Concept | Meaning |
|---|---|
abstract class |
Class that cannot be instantiated |
abstract method |
Method without a body |
| Concrete method | Normal method with a body |
extends |
Subclass inherits abstract class |
| Constructor | Allowed |
| Instance fields | Allowed |
| Static fields/methods | Allowed |
| Multiple inheritance | ❌ One superclass only |
| Interface implementation | Can implement interfaces |
| Incomplete subclass | Must also be abstract |
Easy Rule
Abstract class = shared code + shared state + some methods that subclasses must implement.
For example:
Vehicle
abstract class
/ \
Car Bike
↓ ↓
move() move()
The key difference from a normal class is simply:
You cannot create an object of an abstract class, and abstract methods force subclasses to provide specific behavior.