Can the constructors be inherited in Salesforce Apex - salesforce

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

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

Function declaration in React Components matters?

Here is my question: What is the difference between these two declarations:
Whats the matter if I declare it on top of my component and what if within it?
Thank you in advance.
Functions that are created inside class components are usually referred to as methods.
If your function doesn't depend on class instance properties state or props and is independent of the component you can define that function outside the class.
But if your function need access to the class state, prop or any other class methods, you'll need to define it within the class component so you can use this.state or this.props.
So, thumb rule would be that if you need any access of the class component instance i.e. this within your function, you will need to define the function inside the component.

Missing constructor body inside Class

I am reading type definition of react in here. Inside the code, I found inside class Component {} (in line 396), there is constructor without body as below (line 435 ):
constructor(props: Readonly<P>);
Why there is no implementation of constructor inside a class. Can someone explain?
you are reading index.d.ts
its only typescript definition for javascript implementation.
read more here
Since React is what instantiates the class for you, the type definition is simply informing you of what would be passed to the constructor if you decided to provide one.
This is also useful because it instructs you how to call super(). If you were to create a constructor, you would need to pass the received props to super even if your class did not need them, e.g.
constructor(props) {
super(props);
// your constructor logic here
}

Cakephp - automatically run app model callback with models callback

I have callback functions in AppModel.php, and also for some models, is there is way to automatically call the app model callback with (preferably before) the current model's callback is being called.
e.g. lets say I have beforeSave in AppModel, for each beforeSave function in my models I have to put
parent::beforeSave($options) in it. Now, can I make it at once for all models, so I will not have to put in each callback in each model.
Thanks
Nope.
Because your Model extends AppModel, any of these callback functions will override the parent function. You will always have to manually call the parent function.

How to access controller method in another controller in cakephp?

I have extended class A in class B and i want to use class A methods in class b.
To share a method between two controllers, place the function in AppController.
To share a group of method related to a single feature, create a new component and use it in both controllers. It's the proper OOP way to do it.

AppController vs Component

I have a set of functions shared between all my controllers and I'm doubting whether I should place them in a component, loaded from every controller (or from AppController), or add them in the AppController class (with visibility set to protected), so all the controllers inherit them.
Which is the better?
Creating a component is recommended, IMO. Components are lazy loaded and also help keep your code look clean. Also in use cases where you need access to some sort of model data, you DO NOT want to load models and call them from AppController!

Resources