Sunday, July 14, 2024

Demystifying PowerShell: Method Overloading, Inheritance, and Type Casting

PowerShell, with its object-oriented features, allows developers to create robust and flexible code. In the first article in this series, Object Oriented PowerShell, I discussed the basics of classes in PowerShell. In this post, we’ll explore a couple of code snippets that showcase method overloading, method overriding and inheritance. Our journey will take us through the intricacies of PowerShell classes and their behaviors.

The Code

Here we have 2 classes one derived from the other. As you can see, some of the methods in the base class are overridden in the derived class.
class Base
{
    [void] Foo()
    {
        Write-Host 'Base.Foo'
    }

    [void] Bar()
    {
        Write-Host 'Base.Bar'
    }

    static [void] Bar2()
    {
        Write-Host 'static Base.Bar2'
    }

    [void] Bar2([int] $i)
    {
        Write-Host "Bar2 int = $i"
    }

    # Overloaded method in the same class
    [void] Bar2([double] $f)
    {
        Write-Host "Bar2 double = $f"
    }
}

class Derived : Base
{
    [void] Foo()
    {
        Write-Host 'Derived.Foo'
    }

    [void] Baz()
    {
        Write-Host 'Derived.Baz'
    }

    static [void] Bar2()
    {
        [Base]::Bar2()
        Write-Host 'static Derived.Bar2'
    }

    # Overloaded method in the derived class
    [void] Bar2([string] $s)
    {
        Write-Host "Bar2 string = $s"
    }
}

Let's now call some of the methods and see if the behavior is what we would expect. Let's start with making an instance of the base class and calling its methods. 
# Create an instance of Base
$base = [Base]::new()
# Call the methods of Base
$base.Foo()
$base.Bar()
[Base]::Bar2()
# Here we are calling the overloaded Bar2 method
$base.Bar2(5)
$base.Bar2(5.3)
And the results are what we would expect.
Base.Foo Base.Bar static Base.Bar2 Bar2 int = 5 Bar2 double = 5.3
Now let's do the same with an instance of Derived.
# Create an instance of Derived
$derived = [Derived]::new()
# Call the methods of Base. These all behave as expected
$derived.Foo()
$derived.Bar()
$derived.Baz()
[Derived]::Bar2()
$derived.Bar2(5)
$derived.Bar2(5.3)
$derived.Bar2("Hello World")
And the results are also what we would expect. Ther are some things to note. The most obvious one is that we call the static method Bar2() in Base as part of the call to the overridden static method in Derived. The syntax for this is straight forward. However, this is not way to call an overridden non-static method in a base class.

Additionally, we can add more overloads to a function in a derived class, as we did with the function Bar2 that accepts a string in Derived. 
Derived.Foo
Base.Bar
Derived.Baz
static Base.Bar2 # we correctly call the base classes Bar2 method here
static Derived.Bar2
Bar2 int = 5
Bar2 double = 5.3
Bar2 string = Hello World # This overload only existed in Derived and is called correctly here
One other thing to note, is that you can call a method in a base class that only exists in a derived class. This provides a way to provide a base class that you are required to derive from. Here is an example of that.
class Base
{
    [void] Foo()
    {
        $this.Bar() # Note there is no Bar method in Base
    }
}

class Derived : Base
{
    [void] Bar()
    {
        Write-Host "Derived.Bar"
    }
}
And the expected output. 
Derived.Bar
A few things to note here, as before if there is a hierarchy of classes, the method on the most derived class will be called. This example also shows how to call member methods (or access member data) it is done via '$this.'.

Conclusion

As you can see from this post and the last one, PowerShell provides the ability to write Object-Oriented code. There are, as noted, some limitations, such as the lack of true private methods and data.

PowerShell’s object-oriented capabilities empower developers to build expressive code. Understanding method overloading, inheritance, and type casting is essential for creating maintainable and efficient classes.

Feel free to ask any questions or share your thoughts! 😊

No comments:

Post a Comment

The 2024 State of DevOps Report and the Importance of Internal Development Platforms

On the State of DevOps Report The State of DevOps Report, published annually by the DevOps Research and Assessment (DORA) team, has been a c...