Sunday, May 20, 2018

Method modifiers

The method modifiers are appended to the method definition and give information to the compiler of how these methods should behave. There are three kind of modifiers; OO modifiers, functional modifiers and compiler-hint modifiers.

The Object Oriented modifiers
The OO modifiers specify the inheritance behavior of the method:
abstractabstract means that the method will not be implemented in this class implementation; the class should be specialized with inheritance where this method should be overridden. An abstract method is per definition also a virtual method.
virtualvirtual means that a descendant class can override (or in other words: re-implement) this method.
overrideoverride means that this method re-implements the virtual method of an ancestor class.
finalfinal means that this virtual method can not be overridden in any descendant class anymore.
reintroduce reintroduce means that the method is not re-implemented, but replaced in this descendant class (this is the Shadow modifier in Visual Basic).

Behavior  differences between an overridden method and a reintroduced method
We have two classes; parent and child. Child inherits from parent.

In the parent class, the following method is defined:
This method always returns 1 (how the implementation works is the subject of the next post).

1. Results with override
In the child class we override this method and implement it so it will return 2:

When we create an instance of parent and call the method, it will return 1.
When we create an instance of child and call the method, it will return 2.
 
And when we cast the instance of child to parent, and call the method it will return 2.
This works because child replaced the virtual method with it's own implementation.

2. Results with reintroduce
In the child class we override this method and implement it so it will return 2:

When we create an instance of parent and call the method, it still returns 1.
When we create an instance of child and call the method, it still returns 2.
 
And when we cast the instance of child to parent, and call the method it will now return 1.
And that is because the method in the parent implementation is not replaced by the new implementation in the child class.

The functional modifiers
These modifiers specify functional behavior of the method:
emptyempty means that the method will not be implemented in this class implementation, but in contrast to the abstract modifier, this empty method can be called and is not virtual by default.
asyncthe async modifier marks the method as an asynchronous method that will run in the background.
lockedThe locked modifier changes the behavior of the method in such a way that it becomes thread-safe; only one thread at a time can call this method. If more threads want to call this method simultaneously, the threads have to wait for each other.
locked on <expression>The locked on modifier behaves the same as locked, but works on an expression. If more methods or properties use the same expression than only one thread can run one of these methods or properties at once.
iteratorThe iterator modifier marks the method as a dynamic iterator (that has to yield it's results).
Async, locking and iterators will be the subject of future posts where I will explain them further.


The compiler-hint modifiers
These modifiers help the compiler to generate the correct code for this method:
implements When this method implements an interface method - but has another name than expected within the interface.
The compiler hint implements IInterfaceName.MethodName tell's the compiler which interface method is being implemented here.
inlineThe inline modifier tels the compiler to inline this method instead of generating a function call.
partialThe partial modifier is used in partial classes. if one partial class has a method defined as partial; empty; then a second partial class of the same class can implement this method with the partial modifier.
externTells the compiler that this method is implemented in an external library.
raisesJava caller only.
Tells the java caller which exceptions could be raised by this method.
unsafe.Net only.
Tells the compiler that this method is doing unsafe operations like pointer operations. This modifier makes sure that the heap is not being compressed or garbage collected during the execution of this method.
Partial classes, external libraries and unsafe code will be the subject of future posts where I will explain them further.

The next post will cover the method implementation.


No comments:

Post a Comment