Can the constructors be inherited in Salesforce Apex

Viewed 347

Is there a way to inherit the constructor on a parent class, so that we don't have to define the constructor again in the child class.

Something similar to the Salesforce Apex Exception class, where we can define the child exception classes and use them without having to define any constructor

1 Answers

Yes, you can call the parent class's constructor using super().

Like that (without any parameters) or with whatever you've defined in the parent class (super('These are', 1, 'my awesome arguments', System.today());

From what I remember - if you call parent's constructor - it has to be 1st line in your constructor.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_super.htm

You can also call super.parentMethodName() to call methods other than constructors.

Related