SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex Inheritance Simplified

Apex inheritance or Inheritance is the concept of Object-Oriented Programming Language which allows code in one class to be reused in other classes.

Let’s understand Apex Inheritance with Cars.

A car can be extended to a sports car. At the most basic level, a sports car will have 4 wheels and an engine with a metallic body.

Thus, we can say a sports car has inherited basic car features and has made few modifications to it to have new features.

Apex Inheritance behaves in the same way too.

There will be a top-level entity that will be extended to access the attributes and behaviors of the parent component by the child components.

As in the car example, we can say that the sports car extends the Car to have the basic blocks and later it extended the functionality to have its own features.

Let’s create a Car class and see inheritance in action.

public virtual with sharing class Car {
    public void coreComponents() {
        System.debug('Should have an engine');
        System.debug('Should have a seat belt');
    }

    public virtual void acceleration() {
        System.debug('Top speed of 140kph');
    }
}

In the above code block, we have created one Apex class with the name Car. This is our parent or base class.

As this Car class will be extended by Child classes so we have used the keyword virtual while declaring the class.

Our Car class has the basic properties of a car and with a method acceleration() which shares the information for the top speed by the car.

public with sharing class SportsCar extends Car{
    public override void acceleration() {
        System.debug('Top speed of 240kph');
    }
}

In the above code block, we have created a SportsCar class which is extending the base class – Car. Here we are using the keyword extends to declare that SportsCar class is extending or inheriting the base class Car.

So what we did over here?

We have extended the Car class and redefined the method acceleration(). This is also known as method overriding. Also, the child class has access to all the properties and methods of the parent class.

Please note that if a member is declared as private then it can’t be inherited in the child class. Also, a method can be only overridden if it’s declared as virtual. In the Car class, the method – coreComponents() can’t be overridden by the child SportsCar class.

Can’t we create a separate SportsCar class and implement the method accordingly? Why go and extend from a parent class in the first place?

Let’s answer it in this way. Suppose you have the technology for creating a car so you will use that existing technology and modified a bit to create an advanced sports car.

Similarly in object-oriented programming inheritance is being used. You create a base type and then the child class extends the parent class and writes their own implementation for specific features allowing them to make use of code reusability without writing everything from the scratch.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *