Apex is a strongly typed, object-oriented programming language developed by Salesforce. Inheritance is an OOPs concept in Apex where the child class inherits the properties and methods from the parent class.
In this Salesforce tutorial, I will learn about inheritance in Salesforce Apex. We will also learn about the virtual, extend, and super keywords in Salesforce Apex.
What is Inheritance in Salesforce Apex?
Inheritance is an object-oriented programming concept in Salesforce Apex that allows us to inherit the properties or methods of a class. The base class acts as a parent, and the class that inherits it is called the child class.
Inheritance is helpful as it prevents us from writing similar code repeatedly. Instead of writing the same code, we can call a method from another class.
In Salesforce, we have only a single inheritance. In single inheritance, a class can extend to only one parent class. To implement multiple inheritance, we need to implement multiple interfaces.
Now, let’s take an example of a car company. Suppose the car is a base (parent) class. In that class, we added information such as car details, which are nearly identical across all car variants. So, in this class, we created a method that stores car details separately.
We can have different classes for car models and need to store information about them, but here, we do not need to re-enter the same information repeatedly. Instead, we can call the method from the car class and access that information.
What are virtual, extends, and super Keywords in Salesforce Apex?
virtual Keyword: The virtual keyword is used to define a class or method that can be extended or overridden by child classes. A virtual method can be redefined in a subclass to provide a specific implementation while maintaining the original method’s basic structure.
extends Keyword: The extends keyword is used to create a subclass that inherits properties and methods from a parent (or base) class. When a class extends another, it inherits the methods and variables of the parent class, allowing for code reuse and logical organization.
super Keyword: The super keyword refers to methods and constructors of a parent class within a child class. It will enable the child class to call the method or constructor from a base class, allowing the parent class’s functionality to be reused while implementing or modifying the child class.
Syntax of Creating Inheritance in Salesforce Apex
Create an Apex class and give it a name according to your requirements. When you want to extend the class or create an inheritance, make sure you add the virtual keyword before the class.
public virtual class BaseClass {
public void methodName() {
//logic;
}
}Then, create another class extending the base class you created. You need to add the extends keyword before entering the baseclass.
public class newClass extends BaseClass {
public void methodName1() {
//logic;
}
}Implement Inheritance in Salesforce Apex
In the Apex code below, I will explain how to implement inheritance in the Salesforce Apex class and utilize base class methods to avoid repetitive code.
Here, we created a class with a virtual keyword and a method with car details, which are similar in almost every car.
public virtual class Car {
public void carDetails() {
String brand = 'TATA';
Integer doors = 4;
boolean isAvailable = true;
Decimal MaxSpeed = 200;
Decimal FuleCapacityInLTR = 40;
system.debug ('Car Brand : ' +brand);
system.debug ('Number of Doors : ' +doors );
system.debug ('Available for rent or not : ' +isAvailable );
system.debug ('Car Maximum Speed : ' +MaxSpeed);
system.debug ('Car Fule Capacity : ' +FuleCapacityInLTR );
}
}Then, we created another class to display the information about a particular car, which was inherited from the car class using the extends keyword.
Here, we created a method in which the variable stores the car details, and then, using the super keyword, we called the carDetaild() method, which is from the base(parent) class.
That means we don’t need to write code for the information that we declared in the base class. We can call that method using the super keyword.
public class Altroz extends car{
public void Altroz() {
String Name = 'Altroz';
String Color = 'Orange';
Date Manufactured = Date.newInstance (2020,01,18);
Decimal Price = 735000;
super.carDetails();
system.debug ('Car Name : ' +Name);
system.debug ('Car Color : ' +Color);
system.debug ('Car Date Manufactured : ' +Manufactured);
system.debug ('Car Price : ' +Price);
}
}Similarly, create another class to display different car details. By extending the car class, we can access its methods.
public class Tiago extends car{
public void Tiago() {
String Name = 'Tiago';
String Color = 'White';
Date Manufactured = Date.newInstance(2018,09,05);
Decimal Price = 1000000;
super.carDetails();
system.debug ('Car Name : ' +Name);
system.debug ('Car Color : ' +Color);
system.debug ('Car Date Manufactured : ' +Manufactured);
system.debug ('Car Price : ' +Price);
}
} In the output, you can see that even though we didn’t add the details that we added in the car class, those details are displayed in the child class because we called it the base class method using the super keyword.

In this way, we can implement inheritance in Salesforce Apex.
Conclusion
I hope you have got an idea about inheritance in Salesforce Apex. In this tutorial, we have seen what inheritance means, the uses of virtual, extends, and super keywords, and how we can use Apex classes. After that, I explained the inheritance syntax and showed how to implement inheritance in Salesforce Apex.
You may like to read:
- Object-Oriented Programming in Salesforce Apex
- Trigger Handler Pattern in Salesforce Apex
- Abstraction in Salesforce Apex
- Polymorphism in Salesforce Apex
- Batch Apex in Salesforce With Examples
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.