contact@itechcloudsolution.com +91 997 9933 595 +91 972 6015 295
209-210-211, Western Plaza, Simada Naka, Surat, Gujarat, India 395006.
iTechCloud Solution
Book a call→
Development

Virtual Classes and Virtual Methods in Apex

Apex, Salesforce’s strongly typed, object-orientated programming language, supports core OOP principles like inheritance, polymorphism, and encapsulation. Among these, virtual classes and virtual methods provide the foundation for building scalable, extensible, and testable applications. If you’re aiming to write flexible and future-proof Apex code, understanding these features is a must.

Subhash Panchani headshot
Subhash Panchani
Founder & CEO · iTechCloud Solution
·5 min read
Virtual Classes and Virtual Methods in Apex cover image

In this blog, we’ll explore what virtual classes and virtual methods are, when and how to use them, and the advantages they bring to Apex development.

Table of Contents

What Are Virtual Classes in Apex?What Are Virtual Methods in Apex?Example Use Case: Polymorphism with Virtual ClassesBenefits of Virtual Classes and Methods in ApexBest Practices for Virtual Classes and Methods in ApexConclusion:

What Are Virtual Classes in Apex?

A virtual class in Apex is a class that can be extended by other classes. By default, Apex classes are not inheritable, meaning they cannot be used as a base class unless they are explicitly declared with the virtual ‘virtual’ abstract keyword.

Syntax:

public virtual class Animal {public virtual void speak() {System.debug(‘Animal speaks’);}}

What Are Virtual Methods in Apex?

A virtual method in Apex is a method that can be overridden by its subclass. Marking a method as virtual indicates that you intend it to be extended or customized in a child class.

Syntax:

public virtual void speak() {System.debug(‘Animal speaks’);}

Example Use Case: Polymorphism with Virtual Classes

Let’s look at a scenario where virtual classes and methods shine.

Step 1: Define a virtual base class

public virtual class Notification {public virtual void send() {System.debug(‘Sending a generic notification’);}}

Step 2: Extend with specific behavior

public class EmailNotification extends Notification {public override void send() {System.debug(‘Sending an email notification’);}}

public class SMSNotification extends Notification {public override void send() {System.debug(‘Sending an SMS notification’);}}

Step 3: Implement polymorphic behavior

List notifications = new List{new EmailNotification(),new SMSNotification()};

for (Notification n : notifications) {n.send(); // Calls the appropriate send method}

Virtual classes and methods in Apex offer a powerful design pattern that enhances application flexibility, maintainability, and performance by leveraging core object-orientated principles. Here’s a breakdown of their key benefits:

1. Code Reusability

Developers avoid repeating code in multiple classes. Only when specific behavior needs to change does the child class override the virtual method. This keeps the code DRY (Don’t Repeat Yourself) and ensures consistent behavior across implementations.

2. Scalability

Virtual classes make it easy to extend application functionality. When new requirements arise, developers can introduce new child classes that inherit from the virtual base class, overriding only what’s necessary. This allows projects to grow and evolve without restructuring existing code, which is critical in large or rapidly changing environments.

3. Polymorphism

Virtual methods are enabling polymorphism. Developers can interact with child objects using their base type reference, allowing dynamic method execution based on the actual class instance. This provides flexibility and simplifies logic when dealing with multiple class implementations that share the same base contract.

4. Testability

Virtual methods can be easily mocked or stubbed. This allows developers to isolate behavior and focus tests on specific logic without relying on the full implementation. It leads to cleaner, more reliable tests that are easier to maintain and less brittle.

5. Decoupling

Virtual classes encourage modular design, where components interact through abstractions rather than tightly coupled implementations. This separation of concerns reduces dependencies, making the system easier to understand, maintain, and extend.

In Apex, virtual classes and methods play a key role in designing scalable and extensible applications. A virtual class can be extended, and a virtual method can be overridden by its subclass, making them ideal for scenarios where customisation or dynamic behavior is required. Here’s a summary of best practices when working with them:

1. Design for Extensibility

Virtual classes are the foundation of a plug-and-play architecture. When designing a system that others might extend or customise, define the base logic in a virtual class and allow customisation through overriding methods. This is particularly useful for managed packages or frameworks.

2. Use Abstraction Strategically

Combine this virtual with abstract when you want to force child classes to implement certain methods. This helps enforce contracts in your code while still allowing shared logic in the base class. Always define common behavior in the virtual base class to reduce redundancy.

3. Override Thoughtfully

Override methods only when necessary. Always call super.methodName() inside the overridden method when you want to preserve base functionality. Avoid breaking the flow unless you have a specific need to change behavior.

4. Follow Naming and Documentation Standards

Clearly document which methods are meant to be overridden and what is expected from child classes. Use meaningful names and describe their purpose. This improves code readability and makes the intention clear for other developers.

5. Avoid Overcomplication

Don’t overuse virtual classes and methods. Use them when there’s a genuine need for flexibility or variation in behavior. Unnecessary abstraction can lead to complexity and make the codebase harder to maintain.

6. Test Subclass Behavior

Always write unit tests for both the base virtual class and any extending classes. This ensures that overridden methods behave correctly and do not unintentionally disrupt the expected outcomes.

7. Use in Frameworks and Flows

Virtual classes are excellent for Flow actions and Apex-based frameworks, where you might inject custom logic via Apex plugins.

Virtual classes and methods in Apex are powerful tools for building flexible, reusable, and testable code. They enable developers to define base classes that can be extended and customized through inheritance, promoting clean architecture and code maintainability. By using the virtual keyword, you allow methods to be overridden in derived classes, enabling polymorphic behavior and dynamic method execution.

Key takeaways
Subhash Panchani headshot
Written by
Subhash Panchani
Founder & CEO · iTechCloud Solution

Founder and CEO of iTechCloud Solution, a Salesforce consulting and product partner since 2017. He sets engagement strategy and stays the commercial owner of every account, working with clients across Salesforce, AI and data from the first call through to go-live.

Connect on LinkedIn
Put it to work

Want this applied to your org?

Book a free discovery call and we'll map the highest-ROI next step for your team.

Book a call →

Related insights

All articles →
15 Common Implementation Mistakes That Cost Companies Time & Money
News & Insights
Jun 30, 2026 · 15 min read

15 Common Implementation Mistakes That Cost Companies Time & Money

Read article →
8 Ways to Ensure Salesforce Adoption Rate Stays Above 80%
News & Insights
Jun 30, 2026 · 8 min read

8 Ways to Ensure Salesforce Adoption Rate Stays Above 80%

Read article →
The 5 Biggest Salesforce Risks You Can't Ignore in 2026
News & Insights
Jun 29, 2026 · 4 min read

The 5 Biggest Salesforce Risks You Can't Ignore in 2026

Read article →

Have a similar problem?

Send us the shape of it. We will tell you honestly whether we are the right team.

  • Response within one business day
  • Salesforce-certified architects on the call
  • Delivery across US, UK, EU, Middle East and APAC