Java
Abstract Class
Programming
Object-Oriented Programming
Java Development

Abstract class in Java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In the realm of object-oriented programming, Java employs several mechanisms to promote code reusability and design robustness. One such concept is the "abstract class." Abstract classes are crucial for designing frameworks and APIs, particularly when you want to establish a common protocol for a set of subclasses.

An abstract class in Java is a class that cannot be instantiated on its own and must be inherited by other classes. It is defined using the abstract keyword. Abstract classes are partially implemented classes, meaning some parts of the class might be fully implemented, while others are left for the subclasses to implement. They can feature a mix of methods with complete implementations (concrete methods) and methods that have no implementation (abstract methods).

Purpose and Use-Cases

Abstract classes are primarily used for:

  • Providing a base template for other classes. This template includes common methods (fully implemented) and outlines (abstract methods) that other classes must customize.
  • Encapsulating a common logic that can be shared across multiple subclasses, reducing code duplication.
  • Serving as a superclass solely for inheritance and not for instantiation, which helps to safeguard against creating inappropriate instances of the base class.

Key Characteristics

Below are some essential characteristics of abstract classes in Java:

  • Instantiation: Abstract classes cannot be instantiated directly. Attempting to instantiate an abstract class results in a compile-time error.
  • Inheritance: They can be subclassed by other classes, but any subclass must implement all the abstract methods of the superclass unless the subclass is also declared abstract.
  • Abstract Methods: These are methods declared without an implementation. The implementation of these methods must be provided by the subclass(es).

Syntax and Examples

Here is a simple example of an abstract class in Java:

java
1abstract class Animal {
2    abstract void makeSound();
3
4    void breathe() {
5        System.out.println("Breathing...");
6    }
7}
8
9class Dog extends Animal {
10    void makeSound() {
11        System.out.println("Bark");
12    }
13}

In the above example, Animal is an abstract class with one abstract method makeSound() and one concrete method breathe(). The Dog class extends Animal and provides an implementation for the abstract method makeSound().

Differences Between Interfaces and Abstract Classes

While both interfaces and abstract classes are used to achieve abstraction in Java, they serve different purposes and are used in different scenarios. Before Java 8, interfaces could only have abstract methods, but now they can also contain default and static methods. Abstract classes, on the other hand, can have a constructor and can maintain state.

FeatureAbstract ClassInterface
Method implementationCan have both abstract and concrete methodsOnly abstract methods before Java 8; default and static methods allowed since Java 8
ConstructorsCan have constructorsCannot have constructors
Access modifiersCan have any access modifiersMethods are public by default
Multiple inheritanceNo, can extend only one classYes, can implement multiple interfaces

Conclusion

Abstract classes are a fundamental concept in Java, useful for enforcing a template for other classes and extracting common code. The utility of abstract classes becomes prominent in large software systems where maintaining and extending the codebase efficiently is critical. Understanding when and how to use abstract classes, in contrast to interfaces, is pivotal for designing effective Java applications.

By keeping these distinctions and uses in mind, developers can leverage abstract classes to build flexible and scalable object-oriented systems.


Course illustration
Course illustration

All Rights Reserved.