Inheritance in Apex

|
| By Webner

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.

Note: 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.
Apex
Apex-02
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.
Apex-03
Apex-04

Leave a Reply

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