Inheritance in Apex

Inheritance in Apex
Inheritance in Apex

Inheritance is a feature of OOPS that allows us to inherit the properties or methods of a class. The base class acts as a parent, the class that is inheriting is called the child class. The child class has access to public and protected methods or variables. Just like every programming language, it is possible and easy to use Inheritance in Apex too. Inheritance is useful as it prevents us from writing similar code again and again, instead of writing the same code, we can simply call a method from another class.

Now, let’s come to the implementation part and see how we can use this concept in our code. We simply use the Virtual keyword to specify that the class we are defining is going to be a parent class and the child class will need to use the extends keyword with the parent class name to inherit its properties. In other words, we need to specify which class is going to act as the base and which class will be inheriting.

A parent class can not access the methods of its child.
Example:
public virtual class TestParentClass {
public static void TestParentMethod(){
system.debug('This is a debug statement from the Parent Class');
}
}

This is a Parent class that contains a method that simply debugs a statement.
public class TestChild extends TestParentClass {
public static void TestChildMethod(){
TestParentMethod();
}
}

This is a child class that simply calls the method from its parent class and performs the operation which was being performed by the method present in the parent class.

If we use the Execute Anonymous Window and call the method from the child class, we will be able to see that the method is being called successfully and the debug log is debugging the correct statement.

Although it’s possible to call a method from another class even if it’s not related by using the class name and the method name, Inheritance provides us with another feature of overriding the method. A child class can override a parent class’ method if required. This feature is used when we require some properties of a class and some changes are required to be made to the method that we are inheriting, this change can be the change in functionality or addition of new functionality.

To change or add a feature to an inherited method, we use the Override keyword and in the Parent class, we have to specify which method can be overridden by using the Virtual keyword with it.

Example:
public virtual class TestParentClass {
public virtual void TestParentMethod(){
Integer a = 12;
Integer b = 4;
Integer sum = a+b;
system.debug('Sum : '+ sum);
}
}
public class TestChild extends TestParentClass {
public override void TestParentMethod(){
Integer a = 12;
Integer b = 4;
Integer c = 21;
Integer sum = a+b+c;
system.debug('Sum : '+ sum);
}
}

After overriding the method, we create an instance variable using the parent class but assign the value of child class to it. Then we simply call the method using the instance variable.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Originally published at https://blog.webnersolutions.com on January 17, 2022.

--

--

--

Our team in Salesforce is very strong, with in-depth knowledge of Salesforce classic and Lightning development as well as the Service cloud.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Webner Solutions

Webner Solutions

Our team in Salesforce is very strong, with in-depth knowledge of Salesforce classic and Lightning development as well as the Service cloud.

More from Medium

Why you should not use the System Administrator profile for Integration Users in Salesforce

God Mode Deactivated

Rest CallOut in LWC

Rest CallOut in LWC

No Code Heroku And Salesforce Integration

[Salesforce LWC] — Why Salesforce LWC? [Advantages of Lightning Web Component]