Java Abstract classes


Abstract classes in Java are a key concept in object-oriented programming that allows you to define a class that cannot be instantiated on its own but can serve as a base for other classes. They are useful for creating a common interface for a group of related classes while providing some default behavior.

Key Features of Abstract Classes:

  1. Cannot be Instantiated: You cannot create an object directly from an abstract class. It is meant to be subclassed by other classes.

  2. Abstract Methods: An abstract class can have abstract methods (methods without a body) that must be implemented by its subclasses. However, it can also have fully implemented methods (concrete methods).

  3. Concrete Methods: Abstract classes can contain concrete methods with implementations, allowing subclasses to inherit and use these methods.

  4. Constructors: Abstract classes can have constructors, which can be called when a subclass is instantiated.

  5. Access Modifiers: Abstract classes can have any access modifiers (public, protected, private) for their methods and properties.

Syntax of an Abstract Class

Here's the basic syntax to define an abstract class:

abstract class ClassName { // Abstract method (without a body) abstract void abstractMethod(); // Concrete method void concreteMethod() { System.out.println("This is a concrete method."); } }

Example of Abstract Classes

Let's illustrate abstract classes with an example involving a base abstract class Animal and two subclasses Dog and Cat.

Step 1: Define the Abstract Class

// Abstract class abstract class Animal { // Abstract method abstract void sound(); // Subclasses must implement this method // Concrete method void eat() { System.out.println("Animal eats food"); } }

Step 2: Define Subclasses

// Subclass Dog class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } // Subclass Cat class Cat extends Animal { @Override void sound() { System.out.println("Cat meows"); } }

Step 3: Use the Classes in the Main Method

// Main class public class Main { public static void main(String[] args) { // Create instances of Dog and Cat Animal myDog = new Dog(); Animal myCat = new Cat(); // Call methods myDog.sound(); // Output: Dog barks myDog.eat(); // Output: Animal eats food System.out.println(); // Just for better readability myCat.sound(); // Output: Cat meows myCat.eat(); // Output: Animal eats food } }

Explanation of the Example:

  1. Abstract Class Animal:

    • This class contains an abstract method sound(), which must be implemented by any subclass of Animal.
    • It also contains a concrete method eat(), which provides a common implementation for all animals.
  2. Subclasses Dog and Cat:

    • Both Dog and Cat classes extend the Animal class.
    • Each subclass provides its implementation of the sound() method.
  3. Main Class:

    • In the main method, references of type Animal are used to create instances of Dog and Cat.
    • This demonstrates polymorphism, allowing you to call the overridden methods.

Output of the Example:

When you run the above code, the output will be:

Dog barks Animal eats food Cat meows Animal eats food

Advantages of Abstract Classes:

  1. Code Reusability: Abstract classes allow you to define common behavior and attributes that can be reused across subclasses, reducing code duplication.

  2. Encapsulation of Common Functionality: They provide a way to encapsulate common functionality, ensuring that subclasses implement required methods while still being able to inherit shared behavior.

  3. Flexibility: Abstract classes offer a flexible design, allowing you to create a structured class hierarchy while enforcing certain methods to be implemented in subclasses.

Limitations of Abstract Classes:

  1. Cannot be Instantiated: You cannot create objects of an abstract class directly, which may limit some use cases.

  2. Single Inheritance: In Java, a class can only extend one abstract class (single inheritance). However, it can implement multiple interfaces, which can be a workaround for this limitation.

  3. Inflexibility in Method Implementation: If a subclass does not provide an implementation for the abstract methods, it must also be declared abstract, which may not always be desired.

Summary:

  • Abstract classes in Java provide a way to define a common interface for a group of related classes while enforcing certain methods to be implemented by subclasses.
  • They enhance code reusability and maintainability by encapsulating common functionality, making them a fundamental aspect of object-oriented design in Java.