Salesforce : APEX : Class ( Simple/Regular Vs Abstract Vs Virtual )

Varun   Varun 602 Views
APEX : Class ( Simple/Regular Vs Abstract Vs Virtual )
Note : simple/regular means, class without abstract and virtual keyword

Before going into differences between these, understand regular, abstract, virtual methods :
Regular Method  : Has code body or code implementation  and cannot be overridden

Virtual Method    : Has code body or code implementation and can be overridden by extending class method using override keyword

Abstract Method : Doesn't have body or code implementation and can be overridden by extending class method using override keyword



Regular Class:
a) Can be constructed ( can create object ).
b) Can not be extended
c) Can have regular methods
d) Can have virtual methods
e) Can not have abstract methods

Virtual Class:
a) Can be constructed ( can create object ).
b) Can be extended
c) Can have regular methods
d) Can have virtual methods
e) Can not have abstract methods

Abstract Class:
a) Can not be constructed ( can not create object ).
b) Can be extended
c) Can have regular methods
d) Can have virtual methods
e) Can have abstract methods

Note:
i) Extending class can not reduce visibility of super method by overriding it.
ii)Extending class can not change return type of super method by overriding it.
iii)Extending can do method over loading e.g.

Super class has 
public void getName(){
return 'My Name'; 
}

and Extending class can have
public getName(String str){
return str; 
}

iv) class can't extend two classes but can extend one class (virtual/abstract) and implements multiple interfaces.