You need to sign in to do that
Don't have an account?
Thibault 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.
Try
Mustand myCar = new Mustand();
System.debud(myCar.getBrand);
I tried, but it's the same problem...
Kind Regards,
Tyler Hudson
Contact Us - We Can Help!
Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x123
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:
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.
Furthermore, I need some static class variables.
If 'brand' is static, I couldn't use something like:
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.
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