Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

From reading the docs and dev forums, it seems that having multiple controller extensions can offer the ability to override methods, and keep class size/structure simple. Because these are things class inheritance offer as well, when is it best to use one over the other?

Multiple Extensions

Visualforce Page

<apex:page standardController="Account" 
    extensions="ExtOne,ExtTwo">
    <apex:outputText value="{!foo}" />
</apex:page>

ExtOne

public class ExtOne {
    public ExtOne(ApexPages.StandardController acon) { }

    public String getFoo() {
        return 'foo-One';
    }
}

ExtTwo

public class ExtTwo {
    public ExtTwo(ApexPages.StandardController acon) { }

    public String getFoo() {
        return 'foo-Two';
    }
}

Class Inheritance

Visualforce Page

<apex:page standardController="Account" 
    extensions="AccountController">
    <apex:outputText value="{!foo}" />
</apex:page>

AccountController subclass

public class AccountController extends BaseController {
    public AccountController(ApexPages.StandardController acon) {...}

    public override String getFoo() {
        return 'foo-One';
    }
}

BaseController class

public virtual class BaseController {

    public virutal String getFoo() {
        return 'foo-Two';
    }
}
share|improve this question

1 Answer 1

Inheritance denotes a commonality or relationship between objects. Extensions let you mix in completely unrelated functionality in a consistent manner.

For example, an "address lookup" extension might be used to provide connectivity to a third-party service (most likely in conjunction with a component) to provide address auto-completion. You could use this for leads, accounts, contacts, and even custom objects without having to code the same function in each place. This reduces maintenance, code complexity, and makes feature enhancements easier.

Inheritance implies some sort of related common ancestor, or possibly a specialized version of an object. If you were modelling a banking system, you might have a Transaction, a Withdrawal (extends Transaction), and a Deposit (extends Transaction). The base object, the Transaction, might have fields such as RelatedTransaction, Amount, AccountNumber, and so on, with a "virtual method" that would be implemented by the various types of transactions to perform their task.

You could automatically add new functionality to both Withdrawal and Deposit by modifying Transaction, and in some cases, neither class might even need to specifically be aware of the change. By example, if you decided to add a fee to each Transaction, neither Withdrawal or Deposit would need to be aware of the change if Transaction had been properly designed to begin with.

share|improve this answer
    
Thank you. I actually realized that I meant class extension instead of class inheritance but I think the same argument of common ancestry applies. In your example, is there a reason why I shouldn't encapsulate the address lookup functionality into a vf component and a component controller? –  jmrjulian Jul 6 at 21:40
    
Addresses are an attribute of various types of data. People have addresses, and buildings have addresses, but you can't email buildings (the recipient is one or more people). Let's say you also implemented a Finger class (using "finger" to verify email addresses), you couldn't simultaneously inherit from AddressLookup and Finger, so now you'd have to try to figure out if AddressLookup should inherit from Finger, or the other way around. Add in more functions, and you'll risk coding yourself in a corner. –  sfdcfox Jul 7 at 4:05
    
While you could make it a component controller, that's not the same as inheritance. Inheritance means you'd be using the class in the page's "controller" attribute. Components are reusable portions of code similar to extensions; you use both to add in a feature to pages that otherwise have no common base to derive from. –  sfdcfox Jul 7 at 4:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.