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
abivenkatabivenkat 

Inheritance in apex - doubt


I have 3 classes with me. my requirement is i have to access the base class via derived class

1st class is defined as virtual base class for inheritance as shown below  (IncomeTax)

 

public virtual class IncomeTax {
    
    public IncomeTax() {
    }
    
    public virtual string Tax() {
        return '3';
    }
}

2nd class is defined as normal derived class which extends the base class (GovernmentEmp)

 

public with sharing class GovernmentEmp Extends IncomeTax {
    
    public GovernmentEmp() {
    }
    
    public override string Tax() {
        return '5';
    }
}

3rd class is defined as normal derived class which extends the base class (CorporateEmp)

 

public with sharing class CorporateEmp Extends IncomeTax {
    
    public CorporateEmp() {
    }
    
    public override string Tax() {
        return '10';
    }
}

4th is my apex class controller called as ModelPageController. how can i call a method from base class (IncomeTax) in my class. what i have to do to call the method from the base class?  which class i should extend here? 

 

public with sharing class ModelPageController  {
    
    public string PercentVal { get; set; }
    
    public void Percentage() {
        
    }
}

 

does my inheritance method is right or what is mistake am i committing in the above process? please correct my code in which i can extend the classes and use the base class methods via derived class?? (i.e.,) have to access the "IncomeTax" via "GovernmentEmp" or "CorporateEmp" from my controller class called "ModelPageController".. please help me in my simple inheritance functionality..

 

thanks,

abivenkat,

SFDC Learner

Navatar_DbSupNavatar_DbSup

Hi,


You can use super keyboard to achieve this but you have to call that method inside the derived class with super keyword like this:


public with sharing class GovernmentEmp Extends IncomeTax {

public GovernmentEmp() {
}

public override string Tax() {

string st1=super.Tax();
return st1;
}

}


For more details go through the link below:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_super.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

abivenkatabivenkat

 

hi Navatar_DbSup,

 

Thanks for the reply yaar. But i wanna call the Tax() method which is in IncomeTax from ModelPageController class via any of the derived classes (such as, GovernmentEmp or CorporateEmp); 

so, is there any other way to call the method which is in base class from my class (ModelPageController) via derived class??? 

Navatar_DbSupNavatar_DbSup

Hi,
That I know. By using the above super key inside the derived class you can acess the base class method and I have checked this. Use the below code for this and you will you will find the value as 3 which is defined in your base class.
public with sharing class ModelPageController
{
public string PercentVal { get; set; }
public void Percentage()
{

}
public void show()
{
GovernmentEmp i=new GovernmentEmp();
string st=i.Tax();
system.debug('@@@@@@@@@@@@' +st);
}

}

abivenkatabivenkat

 

hi Navatar_DbSup,

 

is there any way to achieve this without using the super keyword

 

 

thanks,

abivenkat,

SFDC Learner