Inheritance in Apex is a mechanism in which one object acquires all the properties and behaviors of a parent object.
The idea behind inheritance is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Apex supports single inheritance, it allows to extend one other class and implement many interfaces. Interface can also extend multiple interfaces
Why use inheritance
- For Method Overriding
- For Code Reusability.
Terms used in Inheritance
- Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
- Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
- Reusability: Reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class.
Syntax
public class Subclass-name extends Superclass-name { //methods and fields }
The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.
Example
public virtual class Vehicle { // Vehicle attribute public String brand = 'Ford'; public void honk() { // Vehicle method System.debug('Tuut, tuut!'); } }
Subclass
public class Car extends Vehicle { // Car attribute public String modelName = 'Mustang'; }
Anonymous Window
Car myCar = new Car(); // Call the honk() method (from the Vehicle class) on the myCar object myCar.honk(); // Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class System.debug(myCar.brand + ' '+ myCar.modelName);
Output
Related Error:
In Salesforce Non-virtual and non-abstract type cannot be extended.
Class must be virtual or abstract for it to get extended.
The basic difference between a virtual and abstratc class is that methods in virtual class CAN be overridden in derived classes, while abstract class methods MUST be overridden.
Abstract Class Example:
public abstract class abscl { public void m1(){ // Defined and Implemented System.debug(' I am in abscl: m1'); } Public void m2(){ // Defined not implemented } // Declared: Not Defined and Not Implemented this should be declared abtract method not implemented body method. public abstract void m3(); }
Sub Class
public class abschd extends abscl{ // the abstract method should be declared in child class using override but object is created in child class only. public override void m3(){ System.debug('I am in absch: m3 '); } }
Reference
http://ankitgsfdc.blogspot.com/2014/08/object-oriented-programming-in-apex.html
https://salesforceprofs.com/abstract-virtual-interface-in-apex/