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
Rabbani sayyed 8Rabbani sayyed 8 

It is possible to have an extension for a custom controller? If yes can anyone share me the some basic code for how to call that both controller and extension in apex class ?

It is possible to have an extension for a custom controller? If yes can anyone share me the some basic code for how to call that both controller and extension in apex class ?
William LópezWilliam López
Hello Rabbani 

Yes its possible, to access methods just call them from the child.

The only thing Its the the Class that its extented need to be Abstract or Virtual, if you need to use the mother class directly use virtual, if you need that one method from the mother be re-defined by the child use Abstract.

I will do an example with Virtual as its easier.
 
public virtual class MotherClass {
	
	public String profileId;
	public String AccountId;
	
	public PageReference Cancel() {
		//This method can be referenced by any child 
		return null;
	}

}
Here is the child
 
public class Child1 extends MotherClass {

	public PageReference Save() {
		
		
		//Here you can call any method in the parent like Cancel
		
		return null;
	}

}

Then you can have a page like this:
 
<apex:page controller="Child1" id="Child1" >
	<apex:form>
	
		<!-- This button will call the method in Child1 -->
		<apex:commandLink value="Save"  action="{!Save}"/>
		
		<!-- This button will call the method in MotherClass -->
		<apex:commandLink value="Exit"  action="{!Cancel}"/>
		
		
	</apex:form>
</apex:page>

or you can have a page that uses the Mother directly, but since there is not Save method in the mother you can only use the Cancel.
 
<apex:page controller="MotherClass"  >
	<apex:form>
			
		
		<!-- This button will call the method in MotherClass -->
		<apex:commandLink value="Exit"  action="{!Cancel}"/>
		
		
	</apex:form>
</apex:page>

I Hope this small example work as an start point.

More documentation here:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_extending.htm

Regards,

​Don't forget to mark your thread as 'SOLVED' with the answer that best helps yo