Abstract Methods
–– An abstract method is a method declared in a class but does not have an implementation in that class.
–– Subclasses must provide implementations for all abstract methods.
–– An abstract class can also contain non-abstract methods.
In the example below, let’s consider an abstract class named Item that represents a generic item, and we want to define a method called calculateProperty() to calculate some property of the item. However, since the calculation of this property may vary for different types of items, we declare the calculateProperty() method as abstract in the abstract class Item. This indicates that any concrete subclass of Item must provide its own implementation of the calculateProperty() method.
// Abstract class
public abstract class Item {
public void printDetails() { System.debug(‘This is an item.’);
}
}
// Constructor
public CarItem(Decimal fuelEfficiency) { this.fuelEfficiency = fuelEfficiency;
}
Decimal distance = 100;
Decimal fuelConsumed = distance / fuelEfficiency;
System.debug(‘Calculating fuel consumption of the car:’ + fuelConsumed);
return fuelConsumed;
}
}
private Decimal distanceTraveled;
// Constructor
public BicycleItem(Decimal distanceTraveled) { this.distanceTraveled = distanceTraveled;
}
System.debug(‘Calculating distance traveled by the bicycle: ‘ + distanceTraveled);
return distanceTraveled;
}
}
public class Main {
public void executeMain() {
Decimal fuelConsumed = carItem.calculateProperty(); System.debug(‘Fuel consumed by the car: ‘ + fuelConsumed);
BicycleItem bicycleItem = new BicycleItem(50);
// Call the calculateProperty method
Decimal distanceTraveled = bicycleItem.calculateProperty(); System.debug(‘Distance traveled by the bicycle: ‘ + distanceTraveled);
}
}
The abstract class Item contains the abstract method calculateProperty() and a concrete method printDetails(). The calculateProperty() method is declared as abstract, meaning it has no implementation in the abstract class and must be implemented in any subclass that extends Item. The printDetails() method, on the other hand, is a concrete method with an implementation in the abstract class. Subclasses can inherit and use this method without providing their own implementation.
We then have concrete subclasses, such as CarItem and BicycleItem, that extend the Item class. Each of these subclasses provides its own specific implementation of the abstract calculateProperty() method.
For instance, the CarItem class represents an item that is a car. It has a private instance variable fuelEfficiency and a constructor to initialize it. The calculateProperty() method is implemented in CarItem by calculating the fuel consumption of the car based on a constant distance.
Similarly, the BicycleItem class represents an item that is a bicycle. It has a private instance variable distanceTraveled and a constructor to initialize it. The calculateProperty() method is implemented in BicycleItem by calculating the distance traveled by the bicycle.
In the Main class, we create instances of both CarItem and BicycleItem. For the CarItem, we calculate and print the fuel consumed by the car, and for the BicycleItem, we calculate and print the distance traveled by the bicycle. This example demonstrates how an abstract method can be used to define a common method signature in
the abstract class and ensure that each concrete subclass provides its own specific implementation, allowing for polymorphic behavior based on the type of item.