Abstract class in Java is a powerful feature that allow us to define common behavior for a set of subclasses. Abstract class in Java is declared with the abstract
keyword, and it cannot be instantiated directly. Instead, It must be subclassed and the abstract methods must be implemented in the subclass.
Abstract class in Java is useful for a variety of reasons. Some of them are given below:
- Define common behavior for a set of subclasses.
- Promote code reuse.
- Force subclasses to implement certain methods.
- Provide a base class for polymorphism.
Following is an example of an abstract class in Java:
abstract class Vehicle {
abstract void move();
void honk() {
System.out.println("Honk!");
}
}
This class defines a method called move()
that is abstract. This means that the method does not have a body, and it must be implemented in any subclass of Vehicle
. The honk()
method is not abstract, so it can be implemented in the Vehicle
class itself, or it can be overridden in a subclass.
Following is an example of a subclass of Vehicle
:
class Car extends Vehicle {
@Override
void move() {
System.out.println("The car is moving.");
}
}
This class overrides the move()
method to provide a specific implementation for that method. When an object of the Car
class is created, it can be used to call the move()
method, which will print the message “The car is moving.”
Abstract classes are a powerful tool that can be used to improve the design and maintainability of your Java code. If we are working on a project that involves a set of related classes, we should consider using abstract classes to define common behavior and promote code reuse.
Benefits of using abstract classes
There are several benefits of using abstract class in Java:
- Abstraction: Abstract classes allow us to abstract away the implementation details of a class, which can make our code more modular and easier to understand.
- Code reuse: Abstract classes can be used to promote code reuse by defining common behavior for a set of subclasses. This can save us time and effort when we are developing our code.
- Polymorphism: Abstract classes can be used to achieve polymorphism, which allows us to treat different objects of different types in a uniform way. This can make our code more flexible and easier to maintain.
When to use abstract classes
Abstract classes should be used when we want to:
- Define common behavior for a set of subclasses.
- Promote code reuse.
- Force subclasses to implement certain methods.
- Achieve polymorphism.
Abstract classes should not be used when:
- We need to create objects of the class.
- We do not need to define common behavior for a set of subclasses.
- We do not need to promote code reuse.
FAQs
What distinguishes an abstract class from a regular class?
An abstract class must be declared with abstract
keyword and can have both abstract and non abstract methods. Abstract methods are declared without an implementation. Regular classes, on the other hand, must provide implementations for all their methods.
When should I use an abstract class in Java?
You should use an abstract class when you want to create a common interface for a group of related classes, and some methods in that interface have common functionality that can be implemented in the abstract class. It’s also used when you want to ensure that certain methods are implemented by subclasses.
Can an abstract class have constructors?
Yes, an abstract class can have constructors. These constructors can be used to initialize common fields or perform other common tasks when creating instances of subclasses.
What happens if a subclass of an abstract class doesn’t implement all the abstract methods?
If a subclass of an abstract class doesn’t implement all the abstract methods defined in the superclass or another abstract class it extends, it must also be declared as abstract. An abstract subclass must either provide concrete implementations for all abstract methods or be declared as abstract itself.
If a subclass is not abstract and does not implement all of the abstract methods from its superclass, the compiler will throw an exception saying that the subclass must either implement all of the abstract methods or be declared as abstract itself.
Can I create an instance of an abstract class?
No, you cannot create an instance of an abstract class using the new
keyword because abstract classes are incomplete and are meant to be extended by concrete (non-abstract) subclasses.
Can an abstract class extend another abstract class?
Yes, an abstract class can extend another abstract class. This is a common practice in creating hierarchies of abstract classes where each level of the hierarchy adds more specific functionality.
Abstract class can extend another abstract class as well as regular (non abstract) class.
What is the difference between an abstract class and an interface in Java?
Both abstract classes and interfaces provide a way to define a contract for classes to follow, but there are key differences. Abstract classes can have constructors and both abstract and concrete methods, while interfaces can only have abstract methods (prior to Java 8). A class can implement multiple interfaces but extend only one abstract class.
Can an abstract class have instance variables (fields)?
Yes, an abstract class can have instance variables (fields) just like regular classes. These fields can be used to store common data that is shared among the abstract class and its subclasses.