0% found this document useful (0 votes)
38 views3 pages

Apex Extending Documentation With Example

The document explains how to extend a class in Apex, allowing a new class to inherit methods and properties from an existing class while also enabling method overriding for specialized behavior. It illustrates polymorphism through examples of the Marker, YellowMarker, and RedMarker classes, demonstrating how extending classes can have additional methods and override existing ones. Additionally, it mentions that interfaces can also extend other interfaces and outlines changes in behavior for API version 50.0 and later regarding accessibility rules.

Uploaded by

Sharif Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Apex Extending Documentation With Example

The document explains how to extend a class in Apex, allowing a new class to inherit methods and properties from an existing class while also enabling method overriding for specialized behavior. It illustrates polymorphism through examples of the Marker, YellowMarker, and RedMarker classes, demonstrating how extending classes can have additional methods and override existing ones. Additionally, it mentions that interfaces can also extend other interfaces and outlines changes in behavior for API version 50.0 and later regarding accessibility rules.

Uploaded by

Sharif Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/24/25, 3:09 PM Extending a Class | Apex Developer Guide | Salesforce Developers

Developers

Extending a Class
Apex Developer Guide / Writing Apex / Classes, Objects, and Interfaces / Classes / Extending a Class

Extending a Class
You can extend a class to provide more specialized behavior.

A class that extends another class inherits all the methods and properties of the extended class. In
addition, the extending class can override the existing virtual methods by using the override
keyword in the method definition. Overriding a virtual method allows you to provide a different
implementation for an existing method. This means that the behavior of a particular method is
different based on the object you’re calling it on. This is referred to as polymorphism.

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.

This example shows how the YellowMarker class extends the Marker class. To run the inheritance
examples in this section, first create the Marker class.

public virtual class Marker {


public virtual void write() {
System.debug('Writing some text.');
}

public virtual Double discount() {


return .05;
}
}

Then create the YellowMarker class, which extends the Marker class.

// Extension for the Marker class


public class YellowMarker extends Marker {
public override void write() {
System.debug('Writing some text using the yellow marker.');
}
}

This code segment shows polymorphism. The example declares two objects of the same type
( Marker ). Even though both objects are markers, the second object is assigned to an instance of the
YellowMarker class. Hence, calling the write method on it yields a different result than calling this
method on the first object, because this method has been overridden. However, you can call the
discount method on the second object even though this method isn't part of the YellowMarker
class definition. But it’s part of the extended class, and hence, is available to the extending class,
YellowMarker . Run this snippet in the Execute Anonymous window of the Developer Console.

Marker obj1, obj2;


obj1 = new Marker();
// This outputs 'Writing some text.'
obj1.write();

obj2 = new YellowMarker();


// This outputs 'Writing some text using the yellow marker.'

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm 1/3
4/24/25, 3:09 PM Extending a Class | Apex Developer Guide | Salesforce Developers
obj2.write();
// We get the discount method for free
// and can call it from the YellowMarker instance.
Double d = obj2.discount();

The extending class can have more method definitions that aren't common with the original
extended class. In this example, the RedMarker class extends the Marker class and has one extra
method, computePrice , that isn't available for the Marker class. To call the extra methods, the object
type must be the extending class.

Before running the next snippet, create the RedMarker class, which requires the Marker class in your
org.

// Extension for the Marker class


public class RedMarker extends Marker {
public override void write() {
System.debug('Writing some text in red.');
}

// Method only in this class


public Double computePrice() {
return 1.5;
}
}

This snippet shows how to call the additional method on the RedMarker class. Run this snippet in
the Execute Anonymous window of the Developer Console.

RedMarker obj = new RedMarker();


// Call method specific to RedMarker only
Double price = obj.computePrice();

Extensions also apply to interfaces—an interface can extend another interface. As with classes, when
an interface extends another interface, all the methods and properties of the extended interface
are available to the extending interface.

Versioned Behavior Changes


In API version 50.0 and later, scope and accessibility rules are enforced on Apex variables,
methods, inner classes, and interfaces that are annotated with @namespaceAccessible . For
accessibility considerations, see NamespaceAccessible Annotation. For more information on
namespace-based visibility, see Namespace-Based Visibility for Apex Classes in Second-Generation
Packages.

DID THIS ARTICLE SOLVE YOUR ISSUE?


Share your feedback
Let us know so we can improve!

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm 2/3
4/24/25, 3:09 PM Extending a Class | Apex Developer Guide | Salesforce Developers

DEVELOPER CENTERS POPULAR RESOUR CES COMMUNITY


Heroku Documentation Trailblazer Community

MuleSoft Component Library Events and Calendar

Tableau APIs Partner Community

Commerce Cloud Trailhead Blog


Lightning Design System Sample Apps Salesforce Admins

Einstein Podcasts Salesforce Architects

Quip AppExchange

© Copyright 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce, Inc.
Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States

Privacy Information Terms of Use Legal Use of Cookies Trust Cookie Preferences

Your Privacy Choices Responsible Disclosure Contact

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm 3/3

You might also like