+ Start a Discussion
Thibault WeberThibault Weber 

Very basic class extension issue

Hi,

 

I have a problem with classes extensions, here a very simple example: 

 

 

public virtual with sharing class Car{

	protected String brand;
	
	public virtual String getBrand(){
		return this.brand;
	}
}

 

public with sharing class Mustang extends Car{

	protected String brand = 'Ford';

}

 

Controller:

Car myCar = new Mustang();

system.debug( myCar.getBrand() ); // return null

 

myCar.getBrand() should return 'Ford' but I get null.

 

Where is the problem?

 

Thanks.

SFDCStarSFDCStar

Try

 

Mustand myCar = new Mustand();

System.debud(myCar.getBrand);

Thibault WeberThibault Weber

I tried, but it's the same problem...

MrTheTylerMrTheTyler
public with sharing class Mustang extends Car{

public Mustang(){
car c=this;
c.brand='Ford'; }

 

Kind Regards,

 

Tyler Hudson
Contact Us - We Can Help!

Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x123

Thibault WeberThibault Weber

Thanks, your solution works.

But it's not really nice for what I have to do, my class variables are supposed to be final and more complex, like:

 

protected final static List<Map<String, String>> standardColumns = new List<Map<String, String>>{ 																										    
new Map<String, String>{'Name'=>'Nom*', 'Required'=>'true', 'Type'=>'String'},                                  new Map<String, String>{'Name'=>'Prénom*', 'Required'=>'true', 'Type'=>'String'}, new Map<String, String>{'Name'=>'Matricule', 'Required'=>'false', 'Type'=>'String'}, new Map<String, String>{'Name'=>'Fonction', 'Required'=>'false', 'Type'=>'String'}, new Map<String, String>{'Name'=>'Département', 'Required'=>'false', 'Type'=>'PicklistDepartement'}, new Map<String, String>{'Name'=>'Mise à disposition (saisir société d\'appartenance)', 'Required'=>'false', 'Type'=>'String'}, new Map<String, String>{'Name'=>'Jeune docteur (oui/non)', 'Required'=>'false', 'Type'=>'Boolean'}, new Map<String, String>{'Name'=>'Cadre (oui/non)', 'Required'=>'false', 'Type'=>'Boolean'}, new Map<String, String>{'Name'=>'Ingénieur/Docteur (oui/non)', 'Required'=>'false', 'Type'=>'Boolean'}, new Map<String, String>{'Name'=>'Diplôme', 'Required'=>'false', 'Type'=>'String'}, new Map<String, String>{'Name'=>'Statut', 'Required'=>'false', 'Type'=>'PicklistStatutSalarie'}, new Map<String, String>{'Name'=>'Pourcentage temps partiel', 'Required'=>'false', 'Type'=>'Percent'}, new Map<String, String>{'Name'=>'Date entrée (jj/mm/yyyy)', 'Required'=>'false', 'Type'=>'Date'}, new Map<String, String>{'Name'=>'Date sortie (jj/mm/yyyy)', 'Required'=>'false', 'Type'=>'Date'}, new Map<String, String>{'Name'=>'Salaire brut annuel*', 'Required'=>'true', 'Type'=>'Currency'}, new Map<String, String>{'Name'=>'Salaire chargé opt1*', 'Required'=>'true', 'Type'=>'Currency'}, new Map<String, String>{'Name'=>'Salaire chargé opt2*', 'Required'=>'true', 'Type'=>'Currency'}, new Map<String, String>{'Name'=>'Salaire chargé opt3*', 'Required'=>'true', 'Type'=>'Currency'}, new Map<String, String>{'Name'=>'Total heures travaillées opt1*', 'Required'=>'true', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures travaillées opt2*', 'Required'=>'true', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures travaillées opt3*', 'Required'=>'true', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures veille opt1', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures veille opt2', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures veille opt3', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures PM brevet opt1', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures PM brevet opt2', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures PM brevet opt3', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures normalisation opt1', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures normalisation opt2', 'Required'=>'false', 'Type'=>'Number'}, new Map<String, String>{'Name'=>'Total heures normalisation opt3', 'Required'=>'false', 'Type'=>'Number'} };
protected final String EXCEL_FILE_NAME  = 'Imports salariés.xlsx';
...
...

 

So my constructor will be ugly...

 

 

Normally I shouldn't have to do that, my first example should work right?

 

 

This is typically an example of first chapters of "OOP for dummies" or something like that... but it doesn't work in APEX?!

 

Thank you anyway for your help, any other solutions would be appreciated.

 

 

 

Thibault WeberThibault Weber

Furthermore, I need some static class variables.

 

If 'brand' is static, I couldn't use something like:

Car c = this;
c.brand = 'Ford';

 

MrTheTylerMrTheTyler

Your first example should not work as you are assuming that by declaring a variable in the sub-class with the same name as a variable in the super class that this implicitly overrides the variable.  I personally have not seen this behavior you are looking for in any OOP language.

 

You don't have to do variable initilization in your constructor.  You can do it right at the variable declaration.

 

 

public with sharing class Mustang extends Car{

  Car c;
  {
    c=this;
    c.brand='ford';
  }

  public Mustang(){

  }
|

 

Not sure exactly what you are trying to do but see Super Keyword and specifically check out the use of "override" in this Extended Class Example

 

 

Kind Regards,

 

Tyler Hudson
Contact Us - We Can Help!

Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x123