Constructor inheritance

Constructor inheritance allows a child class to tweak a constructor declared on a base class without re-implementing the constructor. The base keyword is used to reference the constructor on the inherited class. The constructor on the base class is executed before the constructor in the inheriting class:

class MyBaseClass {
[String]$BaseProperty

MyBaseClass() {
Write-Host 'Executing base constructor'
$this.BaseProperty = 'baseValue'
}
}
class MyClass : MyBaseClass {
[String]$Property

MyClass() : base() {
Write-Host 'Executing child constructor'
$this.Property = 'value'
}
}

It is possible to invoke a constructor in the base class with a different overload by passing arguments to the base keyword:

class MyBaseClass {
[String]$BaseProperty

MyBaseClass($value) {
$this.BaseProperty = $value
}
}
class MyClass : MyBaseClass {
MyClass() : base('SomeValue') { }
}

The arguments passed to the base keyword may be either variable values, such as the parameters for the constructor on MyClass, fixed values, or expressions that invoke other functions and commands.

This form of inheritance only applies to constructors  the same technique cannot be used for methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.216.163