function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DenaDena 

Difference between Abstract and Virtual classes

I am having trouble finding any documentation on:

 

1.  What is the difference between Abstract and Virtual classes (they seem to be the exact same to me)

 

2. What are some scenarios for using Abstract or Virtual classes (wrapper classes comes to mind but are these the right thing to use?)

 

3.  When would you use an Interface instead of an Abstract or Virtual?  I read they are only suppose to be signatures but I saw an example that had a method with code in it too..

Sonam_SFDCSonam_SFDC

Hi Dena,

 

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.

 

The following link gives a good example of when we use Interface vs abstract, go through when you get a chance :

http://social.msdn.microsoft.com/Forums/en-US/8ad621b8-a915-4d7e-89c3-5dbbc47202fd/whats-the-difference-between-abstract-classes-and-interfaces

 

Hope this helps!

tes2tes2
a bit more clairity:

abstract classes can contain methods that don't require an override and methods that do require an override,  since it can contain methods that do require and override it must be extended to be constructed.  useful if you want to share code among several closely related classes that impliment a common interface

virtual classes do not require any methods to be overriden and can be constructed as is without the need of extending. 


http://share-salesforce.blogspot.com/2013/05/salesforce-apex-class-simpleregular-vs.html
vinod kumar ponnadavinod kumar ponnada
hi sonam_sfdc i need proper example regarding Difference between Abstract and Virtual classes
is looking like same.
Using virtual the child class getting method value from parent class using super keyword(super.m();) and override in method function.
abstract class is a class 
method has NO body, declare has ABSTRACT
* if class contains at least one abstract method
declare class has ABSTRACT
and it interact with child class with using override function
parent class
eg:
public abstract class abscl {
public void m1(){ // Defined and Implemented
System.debug(' I am in abscl: m1');
}
Public void m2(){ // Defined not implemented
}
public abstract void m3(); // Declared: Not Defined and Not Implemented this should be declared abtract method not implemented body method.
}


child class
public class abschd extends abscl{
public override void m3(){         // the abstract method should be declared in child class using override but object is created in child class only.

System.debug('I am in absch: m3 ');
}
}

virtual class

child class interact with parent class using extend keyword








 
TomSnyderTomSnyder
Both classes are useful for subtyping and combined with a Interface, the main difference being the abstract is incomplete.

Abstract Classes must be extended and all abstract methods MUST be overridden.  

Virtual class are functional classes and can be instantiated or extended without then need of overriding methods.

you CANNOT do this
MyAbstract absImpl = new  MyAbstract();
or
class MyNewClass extends MyAbstract {}

but you can do this:
MyVirtual vertImpl = new  MyVirtual();

class MyNewClass extends MyVirtual {}
sekhar raj 4sekhar raj 4
public abstract class a
{
public b()
{
system.debug('welcom);
}
private abstract void c();
public class d extends a
{
private override void c()
{
system.debug('country');
}
}
program will come error :-that one constructor invalies:
 
sekhar raj 4sekhar raj 4
hi dena..
am clearly expalin abstract class.....in simple of (Parent child relation );
we know that relation ,,but in programately system understands..language
for exmple:-
public abstract class Dena (// declaraction of class in U r name u r elder in ur family)
{
public abstract void nooneelder()( u only one person elder no one is there)
{
public mother extends Dena (/// this one ur supporting famiy member of mother)
{
void nooneelder()

system.debug('u r loveing mother');
go to object declaraction and complile it
this is simple of abstract class
 
Venkata Subba Harinath PabbisettyVenkata Subba Harinath Pabbisetty
The virtual definition modifier declares that this class allows extension and overrides. You cannot override a method with the override keyword unless the class has been defined as virtual.

The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined.

Refer 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm for more derails.
Venkata Subba Harinath PabbisettyVenkata Subba Harinath Pabbisetty
The virtual definition modifier declares that this class allows extension and overrides. You cannot override a method with the override keyword unless the class has been defined as virtual.

The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined.

Refer 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm for more derails.
Piotr Gajek 8Piotr Gajek 8

Hello, 

I create some brief description of abstract and virtual classes.

Interface

  • To create interface we need to use interface keyword.
  • Interface can provide a layer of abstraction to your code.
  • Interface is an apex class that can contain only method signature, as a result, the body of each method must be empty.
  • An apex class that is using the interface must implement all methods listed in the interface.
  • Interface separates the specific method declaration from its implementation. Therefore you can have different implementation for the same method. Concept behind Interface is that you can change implementation without changing your whole code. Consequently, method signature (return type, parameters) are always the same.
  • Interface can be treated as a new data type. In Apex we have a few predefined interfaces like String, Integer, Double, etc. In other words implementation of those methods can be change (by Salesforce) without changes in our code – that’s the power of interface! e.g.: String.isBlank(‘Test’); We know method signature (public static Boolean isBlank(String inputString)), but we don’t know implementation layer.

Abstract

  • To create abstract class, we need to use abstract definition modifier.
  • Allow to extend the child class.
  • Abstract class can contain methods signed as abstract, to clarify, it is a method that has only a signature (body is not defined).
  • Child class must implement all methods declared as abstract!
  • Abstract class can also include other methods, which have the logic. To allow child class access those methods use protected or public keyword.
  • Cannot be initialize directly: new TestAbstractClass():
  • Abstract class can contain both virtual and abstract methods.
  • Abstract class is some kind of “partial” class. Therefore, some methods are implemented, some needs to be implemented by child class.
  • virtual methods can be override, but this is not mandatory.
  • We can have different signatures for our methods:
  • private – child class doesn’t have access to method signed as private.
  • protected – child class has access to parent class method, but any other class doesn’t have access.
  • public – In other words, making method accessible by any other class.
Virtual
  • To create virtual class, we need to use virtual definition modifier.
  • You can extend a class to provide more sophisticated behaviour.
  • Class that extends another class inherits all the methods and properties of the extended class.
  • Methods declared as virtual can be override. In other words, overriding a virtual method allows you to provide a different implementation for an existing method.
  • A class can only extend one other class, but it can implement more than one interface.
  • Virtual class can be initialize directly new TestVirtualClass();
  • You can use only virtual methods.
  • Virtual class is some kind of “full” class. All methods are implemented, but can be override by child class.

More details you can find here (https://salesforceprofs.com/abstract-virtual-interface-in-apex/).