Category Archives: Fundamentals and Patterns

Difference between Abstract & Virtual class and Interface w.r.t Apex in Salesforce

Let me admit from the beginning that I’ve  a non Java Background. Being in Salesforce development role for more than 3 years, this concept eluded me for some time. There are plethora of examples on the internet but not everyone explains this in simple and lucid words. Also not able to able find single link which explains everything with examples in one single article. Last but not least, I tend to forget things If I don’t write. so making attempt to make this clear for myself (and if possible for everyone), once for all.

Interfaces : Taking definition directly from Salesforce site as it is very clear from there

An interface is like a class in which none of the methods have been implemented—the method signatures are there, but the body of each method is empty. To use an interface, another class must implement it by providing a body for all of the methods contained in the interface. Note Keyword ‘implement‘. Classes implements interfaces ex :

// An interface that defines what a purchase order looks 
//like this in general
public interface PurchaseOrder {
    // All other functionality excluded
    Double discount();
}

This class implements the PurchaseOrder interface for customer purchase orders.

// One implementation of the interface for customers
public class CustomerPurchaseOrder implements PurchaseOrder {
    public Double discount() {
        return .05;  // Flat 5% discount
    }
}

Note few important concepts : 

  1. Interfaces doesn’t have methods with body
  2. Interfaces doesn’t have variables
  3. Methods of interfaces doesn’t have access modifiers(Public private etc)

Abstract class :  This is where I run into problem as not much clear example as well as definition are not there. Also clarity on, its difference from virtual class is also not much available. In my own words :  It is a class which has both defined(with body) & non defined Methods. Other classes extends Abstract Class. If  a class is extending Abstract Class, then it needs(required) to implement all such methods of abstract Class , defined with abstract keyword.

public abstract class AreaofShapes
{
public decimal rad = 0.8;
Public integer length ;
public decimal area{get;set;}

Public AreaofShapes(decimal length) {
// assume our default area calculation is length x length
area = length *length ; 
}
//Note : if this class has parameterized constructor, 
//then it must have 'no parameter constructor'
public AreaofShapes()
{ }
//Note : Abstract class can have Non abstract methods, 
//same not true for interfaces ;
public Decimal circleArea(integer r)
{
area = (r)*(r)*(Math.acos(-1)); // Pi = Math.acos(-1)
return area;
}
// Note: Abstract methods must not define a body
//( not even curly braces), else error //will come up
  // These below methods must be overridden in class 
//implementing this abstract class
public abstract decimal RectangleArea(decimal length, decimal width);
public abstract decimal hexagonArea(decimal length);
}
//Now Example of class extending abstract Class
 public class calculateArea extends AreaofShapes
{
// Note :This class will have automatic access of variables 
// of Abstract class
public calculateArea ()
{
//Calling constructor of parent abstract class using super keyword
super(5);
}
// Calling non abstract method of parent
public decimal Findcirclearea(integer r) {
return circleArea(r);
}
// This class must use override keyword for abstract methods. 
// Note area variable not defined in this class and still used
 //as it is defined in //parent.
public override decimal RectangleArea(decimal length, decimal width)
area = length * width ;
return area;
}
public override decimal hexagonArea(decimal length){
area = 6*length;
return area;
}
//this class can have its own methods also
public decimal calculateSquareArea() {
// I can't just call Constructor of Parent in child 
//in any method ,/ super needs to be present in constructor 
//of child
// area = super(length)// this line will give error,if uncommented
// area calculated by constructor, will be returned
// from below statement.
return area ; 
}
}

Although I’ve written comments to show few important points related to abstract class still,

Few points to note from above example :

  1. Abstract class has  variables as well properties; these are not present in interface
  2. it can have well defined methods , which can’t be present in Interfaces.
  3. If method is defined as abstract, it can’t have body— same as interface but different from virtual class.In virtual class, method defined as virtual, can have body . Read virtual class example below
  4. if method is defined as abstract, it needs(required) to be implemented, same as interface, but different from virtual class. In virtual class, you may or may not implement such methods.
  5. In class extending Abstract Class, you use ‘override keyword( same way as in virtual class) to override method defined as ‘Abstract’ in parent Abstract Class.

Virtual Class : It also has defined and non defined methods. Here also, Other classes extends virtual class but they ‘don’t  need to implement all methods of virtual class, defined with virtual keyword. Example :

public virtual class Player {

String name;

//Note parametrized constructor is present
public Player(String name) {
this.name = name;
}
// If parametrized constructor is present, you need to define
// non parametrized one also.
public Player ()
{ }
// well defined method without  virtual keyword
Public String getName(){
return this.Name;
}
// Method with virtual Keyword. Note, it also has body unlike
// abstract class or interface
public virtual String getGreeting(){
return 'Are you ready to play, ' + this.getName() + '?';
}
}

Now giving example of class extending virtual class

public class footballPlayer extends Player
{
// Note even if this class body is empty, this can be saved,
// so it means class extending virtual class NEEDN'T implements
// all methods of parent defined with  virtual Keyword
//But for abstract class, if method is defined as abstract, 
//it NEEDS to be implemented

public footballPlayer (String name){
super(name);
}
// its my choice, to override a virtual method of parent class. 
//its not necessary.See use of override keyword.
public override String getGreeting(){
return 'It\'s a great day for football, ' + this.getName() + '!';
}
}

Important note ,although i’ve written all important comments in example itself :

  1. Virtual class can have variables unlike interfaces.
  2. virtual class can be empty without any methods.
  3. Virtual class can have well defined methods including constructors.
  4. Classes extending virtual class are not required to implement methods defined with virtual  keyword in Parent.
  5. We use override keyword to override any method defined in parent virtual class.
  6. It can have both defined and non defined methods but generally has  well defined methods. These methods can exist with or without virtual keyword.

I hope with these examples and points, Difference between these should be clear. Please revert in case of any doubt.