concept base class in category powershell

appears as: base class, The base class
Windows PowerShell in Action, Third Edition

This is an excerpt from Manning's book Windows PowerShell in Action, Third Edition.

If you’ve done much object-oriented programming, this concept is similar to creating an override to a virtual method that calls the overridden method on the base class. The difference here is that it’s all instance-based; no new type is involved.

The base class 1 is as shown in listing 19.8 and earlier examples in the chapter. The new class’s relationship 2 to the base class is shown by the first line of the class definition:

The base class 1 is as in the previous section. The derived class 2 has a method with the same signature as the base class. This means it will override, or replace, the method from the base class. The method in the derived class is different in that it calculates the product of the input array 3 rather than the sum. Notice that $result is set to 1 before the calculations start. Multiplying by zero gives zero!

Accessing the base class

You saw how to override a method in the base class in listing 19.9. If you need to use the method in the base class, you can do so. When you create your derived class, add a call to the base class’s method:

PS> class newutils2 : utils {

  [int] Sum([int[]] $na){
     return ([utils]$this).Sum([int[]] $na)
  }
}

You’re casting to the base class so that you can access the method. When you create an instance and use it, you get the sum of the array: