Using the super Keyword
The super keyword can be used by classes that are extended from virtual or abstract classes. By using super, you can override constructors and methods from the parent class.
public virtual class SuperClass { public String mySalutation; public String myFirstName; public String myLastName; public SuperClass() { mySalutation = 'Mr.'; myFirstName = 'Carl'; myLastName = 'Vonderburg'; } public SuperClass(String salutation, String firstName, String lastName) { mySalutation = salutation; myFirstName = firstName; myLastName = lastName; } public virtual void printName() { System.debug('My name is ' + mySalutation + myLastName); } public virtual String getFirstName() { return myFirstName; } }
public class Subclass extends Superclass { public override void printName() { super.printName(); System.debug('But you can call me ' + super.getFirstName()); } }
The expected output when calling Subclass.printName is My name is Mr. Vonderburg. But you can call me Carl.
public Subclass() { super('Madam', 'Brenda', 'Clapentrap'); }
Now, the expected output of Subclass.printName is My name is Madam Clapentrap. But you can call me Brenda.
Best Practices for Using the super Keyword
- Only classes that are extending from virtual or abstract classes can use super.
- You can only use super in methods that are designated with the override keyword.
Various trademarks held by their respective owners.
No comments:
Post a Comment
Thanks for your comment