Table of Contents
Recap of previous Sessions
In the previous blog, we learned “Polymorphic Query in Salesforce” If you have missed the session, please refer to the below link.
Introduction
Inheritance is a fundamental concept in any programming language where the child class can access the properties or methods of the parent class that it’s extending.
If you have a requirement where you want to inherit one class in another class then you need to make that class either virtual or abstract.
You can extend a class to provide more specialized behavior. Below is the example virtual class with virtual methods.
public virtual class Marker {
protected String markerName;
public Marker(String name){
this.markerName = name;
}
public virtual void write() {
System.debug('Writing some text.');
}
public virtual Double discount() {
return .05;
}
}
A class extends
another class using the extends keyword in the class definition. A class can only extend one other class, but it can implement more than one interface.
// Extension for the Marker class
public class YellowMarker extends Marker {
public YellowMarker(String name){
super(name);
}
public override void write() {
System.debug('Writing some text using the yellow marker. '+markerName);
}
}
Important concept of Classes in Salesforce
- Static keyword
- A static variable or method is a class-level construct that is initialized only once and is associated with the outer class, along with its initialization code. Such variables and methods can be accessed directly using the class name, without the need to create an instance of the class.
- Keywords
- Final
- This keyword is used to define constants and methods that can’t be overridden.
- super
- This keyword invokes a constructor on a superclass. Refers to the Variables and Method of a superclass
- this
- This keyword represents the current instance of a class.
- return
- This keyword returns a value from a method.
- null
- This keyword defines a null constant that can be assigned to any variable.
- Final
Method Overloading vs Method Overriding
- Method Overloading
- Does not Require a Parent-Child Relationship
- method name should always be the same
- method return type could be different
- method parameter must be different or method parameter data types must be different
- All the methods should be within the same class
- Method Overriding
- Requires a Parent-Child Relationship
- return type should be the same as the parent class method
- method variables/parameters should be the same as the parent class method
- method name should always be the same
- Uses override keyword in child class
- in parent class, the method must be virtual
What is Polymorphism in Salesforce?
A person at the same time can have different characteristics. Like a man, at the same time is a father, a husband, and a team member. So the same person possesses different behavior in different situations. This is called polymorphism.
What is Method Overloading in Salesforce
If a class has multiple methods having the same name but different parameters, it is known as Method Overloading.
There are two ways to overload the method in Java
- The number of arguments is different in the method
- Changing the data type of the arguments
Example
public class Arithmatic {
public void sum(Integer a, Integer b){
}
public void sum(Integer a, Integer b, Integer c){
}
public void sum(Integer a, Integer b, Integer c, Integer d){
}
public void sum(Integer a, Integer b, Integer c, Integer d, Integer e){
}
}
Hands-On
public virtual class Marker {
protected String markerName;
public Marker(String name){
this.markerName = name;
}
protected virtual void write(){
System.debug(markerName+' is Writing ');
}
protected virtual void errase(){
System.debug(markerName+' is errasing ');
}
protected void paint(){
System.debug(markerName+' is painting ');
}
}
/*
* AWS S3/EC2/Emails/Workflows/Lamda
* Authenticate - Common
* SharePoint
* Authenticate - Common
* Legacy Database
* Authenticate - Common
*/
public class YellowMarker extends Marker {
protected String message;
public YellowMarker(String name, String color, String message){
super(name);
}
public void main(){
this.message = 'This is simple message';
// markerName = RED
super.markerName = 'Yellow';
// markerName = Yellow
this.write();
super.paint();
}
/* Method Overriding */
public override void write(){
System.debug('Marker color is '+super.markerName);
}
}
/*
public virtual class Marker {
public String markerName;
public Marker(String name){
this.markerName = name;
}
public virtual void write(){
String name = 'ABC';
System.debug(markerName+' is Writing ');
}
}
/*
* AWS S3/EC2/Emails/Worflows/Lamda
* Authenticate - Common
* SharePoint
* Authenticate - Common
* Legacy Database
* Authenticate - Common
*/
public class Arithmatic {
public void sum(Decimal a, Decimal b){
}
public void sum(Decimal a, Integer b){
}
public void sum(Integer a, Integer b){
}
public void sum(Integer a, Integer b, Integer c){
}
public void sum(Integer a, Integer b, Integer c, Integer d){
}
public void sum(Integer a, Integer b, Integer c, Integer d, Integer e){
}
}
public virtual class AWS {
// AWS S3/EC2/Emails/Workflows/Lamda
protected String serviceName;
protected String regionName;
protected String url;
protected String accessKey;
protected String accessSecret;
public AWS(String serviceName, String regionName, String url, String accessKey, String accessSecret){
}
protected virtual void authenticate(){
//
}
}
public class S3Service extends AWS {
public S3Service(String serviceName, String regionName, String url, String accessKey, String accessSecret){
super(serviceName,regionName,url,accessKey,accessSecret);
}
}
Watch Complete Videos
Assignment
Create a virtual Class named Vehicle and create the below methods
- start
- stop
- changeGear
Create another class Mercedes that extends the Vehicle class and overrides the above methods within the Mercedes class.
Resources
- https://www.jamessimone.net/blog/joys-of-apex/abstract-classes-and-interfaces/
- https://www.salesforcetutorial.com/static-final-this-super-keywords-in-apex/
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm
- https://www.javatpoint.com/method-overloading-in-java
- https://www.educative.io/answers/overloading-vs-overriding
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm
- https://atrium.ai/resources/what-is-an-interface-in-apex-salesforce-and-how-to-use-it/
- https://beyondthecloud.dev/blog/abstract-virtual-interface-in-apex
- https://salesforce.stackexchange.com/questions/209510/whats-the-difference-between-abstract-class-and-virtual-class