Abstract Methods – Classes, Interfaces, and Methods – Data Manipulation Language (DML) – Salesforce Certified Platform Developer I Study Guide

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 {

  • Abstract method to calculate some property public abstract Decimal calculateProperty();
  • Concrete method

public void printDetails() { System.debug(‘This is an item.’);

}

}

  • Subclass representing a car item public class CarItem extends Item { private Decimal fuelEfficiency;

// Constructor

public CarItem(Decimal fuelEfficiency) { this.fuelEfficiency = fuelEfficiency;

}

  • Implementing the abstract method to calculate fuel consumption public override Decimal calculateProperty() {

Decimal distance = 100;

  • Assume a constant distance for simplicity

Decimal fuelConsumed = distance / fuelEfficiency;

System.debug(‘Calculating fuel consumption of the car:’ + fuelConsumed);

return fuelConsumed;

}

}

  • Subclass representing a bicycle item public class BicycleItem extends Item {

private Decimal distanceTraveled;

// Constructor

public BicycleItem(Decimal distanceTraveled) { this.distanceTraveled = distanceTraveled;

}

  • Implementing the abstract method to calculate distance traveled public override Decimal calculateProperty() {

System.debug(‘Calculating distance traveled by the bicycle: ‘ + distanceTraveled);

return distanceTraveled;

}

}

public class Main {

public void executeMain() {

  • Create an instance of the CarItem class CarItem carItem = new CarItem(10);
  • Call the calculateProperty method

Decimal fuelConsumed = carItem.calculateProperty(); System.debug(‘Fuel consumed by the car: ‘ + fuelConsumed);

  • Call the printDetails method inherited from the Item class carItem.printDetails();
  • Create an instance of the BicycleItem class

BicycleItem bicycleItem = new BicycleItem(50);

// Call the calculateProperty method

Decimal distanceTraveled = bicycleItem.calculateProperty(); System.debug(‘Distance traveled by the bicycle: ‘ + distanceTraveled);

  • Call the printDetails method inherited from the Item class bicycleItem.printDetails();

}

}

  • Instantiate the Main class and execute the main method Main mainInstance = new Main(); mainInstance.executeMain();

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.