Dynamic dispatch is the process of selecting which implementation of a polymorphic method to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented programming (OOP) languages and systems.
Polymorphism in object-oriented programming is the phenomenon where objects of the class expose an operation of the same name but possibly differing in behavior. Dynamic dispatch contrasts with static dispatch, in which the implementation of a polymorphic operation is selected at compile time. The whole purpose of dynamic dispatch is to defer the selection of an appropriate implementation until the run time type of a parameter is known.
A polymorphic operation has several implementations, all associated with the same name. With dynamic dispatch, one particular implementation of an operation is chosen at run time.
In apex programming methods and classes are final by default. You cannot use the final keyword in the declaration of a class or method. This means they cannot be overridden. Use the virtual keyword if you need to override a method or class. The virtual definition modifier allows extension and overrides.
Use final when you know that a declaration does not need to be overridden.
The final keyword is a restriction on a class, method, or property that indicates that the declaration cannot be overridden. This allows the compiler to safely elide dynamic dispatch indirection.
@ Overriding:
- It is an object-oriented programming that enables the child class to provide a different implementation for a method that is already implemented in its parent class.
- This is possible through only inheritance
- Multiple methods containing the same name, the same signature is inherited (virtual) and another is originated (override) in the child class
Lets run a example of dynamic dispatch :
A class that extends another class inherits all the methods and properties of the extended class. The extending class can override the existing virtual methods by using the override keyword in the method definition. Overriding a virtual method allows you to provide a different implementation for an existing method. This means that the behavior of a particular method is different based on the object you’re calling it on. This is referred to as polymorphism.
A class extends another class using the extends keyword in the class definition. A class can only extend one other class, but it can implement more than one interface.
This example shows how the YellowMarker class extends the Marker class. To run the inheritance examples in this section, first create the Marker class.
Then create the YellowMarker class, which extends the Marker class.
This code segment shows polymorphism. The example declares two objects of the same type (Marker). Even though both objects are markers, the second object is assigned to an instance of the YellowMarker class. Hence, calling the write method on it yields a different result than calling this method on the first object, because this method has been overridden. However, you can call the discount method on the second object even though this method isn’t part of the YellowMarker class definition. But it is part
of the extended class, and hence, is available to the extending class, YellowMarker.
Execute anonymous code illustrations :
Performance benefits of declaring Apex variables as Final
The discussion around the topic, declaring Apex variables as Final would fetch good performance benefits is something I would like to show with code examples and their execution out put .
Please carefully examine the code output results, it shows declaring variables as Final would run faster when compared to non final variables .
These are not benchmark results by salesforce but test done on 3 different orgs and test output exhibit similar output pattern .
Below code executed in anonymous Apex
Output :
Conclusion :
The result did not add a ton of processing time in any individual call when run for every thousand calls. But using final here does provide a tangible, measurable benefit, however small.
Comments
Post a Comment