An abstract class in Java is a class that cannot be instantiated directly. It is used as a blueprint for other classes, often providing a base set of functionalities while leaving certain methods unimplemented for subclasses to define.
An abstract class is declared using the abstract keyword.
Key Characteristics of Abstract Classes
- Cannot Be Instantiated: You cannot create an object of an abstract class directly.
- Can Have Both Abstract and Concrete Methods:
- Abstract methods: Declared without implementation.
- Concrete methods: Fully implemented in the abstract class.
- Supports Inheritance: Abstract classes are meant to be extended by subclasses.
- Can Have Constructors: Although you can’t instantiate an abstract class, it can have constructors to initialize fields.
- Can Have Fields and Methods: Abstract classes can contain instance variables, static variables, and methods.
Syntax of Abstract Class
abstract class AbstractClass {
// Abstract method (does not have a body)
abstract void abstractMethod();
// Concrete method (has a body)
void concreteMethod() {
System.out.println(“This is a concrete method.”);
}
}
When to Use Abstract Classes
- Use an abstract class when you want to provide a common base with shared code for related classes.
- Use it when you have some methods that must be implemented differently in derived classes.
Example of Abstract Class
// Abstract class
abstract class Animal {
// Abstract method (no implementation)
abstract void sound();
// Concrete method
void sleep() {
System.out.println(“Sleeping…”);
}
}
// Subclass that extends the abstract class
class Dog extends Animal {
// Implementing the abstract method
@Override
void sound() {
System.out.println(“Dog barks”);
}
}
// Subclass that extends the abstract class
class Cat extends Animal {
// Implementing the abstract method
@Override
void sound() {
System.out.println(“Cat meows”);
}
}
public class Main {
public static void main(String[] args) {
// Animal animal = new Animal(); // Not allowed (abstract class)
Animal myDog = new Dog();
myDog.sound(); // Output: Dog barks
myDog.sleep(); // Output: Sleeping…
Animal myCat = new Cat();
myCat.sound(); // Output: Cat meows
myCat.sleep(); // Output: Sleeping…
}
}
Abstract Classes vs Interfaces
Feature | Abstract Class | Interface |
Methods | Can have both abstract and concrete methods. | All methods are abstract by default (Java 7). From Java 8, interfaces can have default and static methods. |
Inheritance | Supports single inheritance. | Supports multiple inheritance. |
Fields | Can have instance and static variables. | Can only have static final variables (constants). |
Constructors | Can have constructors. | Cannot have constructors. |
Use Case | Used when classes share common behavior. | Used to define a contract for classes to implement. |
Benefits of Abstract Classes
- Code Reusability: Shared behavior can be implemented in the abstract class.
- Encapsulation: Keeps shared behavior and forces derived classes to implement specific functionality.
- Polymorphism: Allows handling of derived classes through a common base class reference.
Abstract classes are a powerful tool for creating extensible and reusable code structures in Java.