EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Abstraction keyword

In Java, the abstract keyword is a non-access modifier applied to classes and methods, but not variables. It is primarily used to achieve abstraction, one of the core principles of Object-Oriented Programming (OOP). Below are the various contexts where the abstract keyword can be utilized in Java.

Characteristics of the abstract Keyword in Java

The abstract keyword is used to define abstract classes and methods. Here are its key characteristics:

  • Abstract classes cannot be instantiated: An abstract class is one that cannot be instantiated directly. It serves as a base class for other classes, which are responsible for providing concrete implementations of its abstract methods.
  • Abstract methods lack a body: An abstract method is declared using the abstract keyword and has no method body. It ends with a semicolon. Any class that extends an abstract class must provide implementations for all abstract methods.
  • Abstract classes can have both abstract and concrete methods: Along with abstract methods, an abstract class can also contain concrete methods with full implementations. These methods can be used by the abstract class itself or its subclasses.
  • Abstract classes can have constructors: While abstract classes cannot be instantiated, they can define constructors. These constructors are typically called during the instantiation of a concrete subclass.
  • Abstract classes can include instance variables: Abstract classes can declare instance variables, which can be accessed by both the abstract class and its subclasses.
  • Abstract classes can implement interfaces: An abstract class can implement interfaces and provide concrete implementations of the interface methods. However, the abstract class does not need to implement all methods immediately—this can be deferred to its subclasses.

Abstract Methods in Java

Abstract methods serve the purpose of declaring methods in a superclass without providing an implementation. Subclasses are responsible for implementing these methods. Abstract methods are often referred to as having “subclass responsibility.”

To declare an abstract method, use the following general syntax:

abstract returnType methodName(parameterList);

Since no method body is provided, any class that extends an abstract class must implement all of the abstract methods.

Rules for Abstract Methods

Some important rules associated with abstract methods are:

  • Any class containing one or more abstract methods must also be declared abstract.
  • You cannot combine the abstract modifier with the following modifiers: finalnativesynchronizedstaticprivate, or strictfp.

Abstract Classes in Java

An abstract class is a class that has partial implementation, meaning it may have methods that lack concrete definitions. To declare a class abstract, use the following syntax:

abstract class ClassName {
    // class body
}

Abstract classes cannot be instantiated directly, and any class that extends an abstract class must either implement all abstract methods or be declared abstract itself.

Example of Abstract Classes and Methods

Here’s an example that demonstrates the use of abstract classes and methods:

// Abstract class representing a general vehicle
abstract class Vehicle {
    // Abstract method (no implementation)
    abstract void startEngine();

    // Concrete method
    void stopEngine() {
        System.out.println("Engine stopped.");
    }
}

// Concrete class representing a car
class Car extends Vehicle {
    // Providing implementation for the abstract method
    void startEngine() {
        System.out.println("Car engine started.");
    }
}

// Driver class to demonstrate abstract classes
public class Main {
    public static void main(String[] args) {
        Vehicle myCar = new Car();  // Vehicle reference, Car object
        myCar.startEngine();        // Output: Car engine started.
        myCar.stopEngine();         // Output: Engine stopped.
    }
}

Output:

Car engine started.
Engine stopped.
End of lesson.