February 2019
Intermediate to advanced
626 pages
15h 51m
English
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 ...Read now
Unlock full access