In-Depth

Understanding PowerShell Class Inheritance

Inheritance will save you a ton of time when creating similar classes by repurposing some of the work you've already done and cutting down on redundancy.

PowerShell v5 introduced a familiar concept to developers called classes. Classes allowed PowerShell developers to finally have a say-so in the kind of objects they'd be building. An important concept in understanding classes is inheritance. Inheritance is something that PowerShell developers did not have access to when just creating custom objects. As of v5, us PowerShellers can now leverage the hierarchical nature of classifying objects through inheritance

A good practice in coding is to use the DRY (don't repeat yourself) method. This means that it's not good to ever have the same kind of code in multiple spots. Instead, focus on "sharing" the code amongst different uses. For example, to create two files it's not good to do this:

New-Item –Path C:\File1.txt
New-Item –Path C:\File2.txt

Instead, it's better to do this:

'C:\File1.txt','C:\File2.txt' | foreach {
New-Item –Path $_
}

Notice that now I only have one New-Item reference.

The same concept can be applied to PowerShell classes. For example, let's say I have a class called Truck with a few properties.

class Truck {
[string]$EngineType
[string]$Color
[string]$BedLength
}
Figure 1.

This has all of the properties I currently need to define a 'Truck'. Now I'd like to define a 'Car' class so I'll use the same approach.

class Car {
[string]$EngineType
[string]$Color
[string]$TopSpeed
}
Figure 2.

This works just fine for me as well. I have all of the required properties. However, do you see a "DRY" problem here? I've used the EngineType and Color properties in two different classes. I'm repeating myself here, which is not good practice to do. Let's redesign these classes to "share" or inherit both the EngineType and Color properties.

First of all, I need to think of a class that will act as the parent. What is a concept that can define both a car and a truck? A vehicle can do that. Let's create a Vehicle class with those common properties.

class Vehicle {
[string]$EngineType
[string]$Color
}

We can now define a generic Vehicle object. Now we need to get more specific and also define the truck and car classes. To do that, I'll set up both classes so that they inherit all of the properties from the Vehicle class.

class Car : Vehicle {
[string]$TopSpeed
}

class Truck : Vehicle {
[string]$BedLength
}

Notice that to tell PowerShell to inherit from a parent class is to use the semicolon followed by the parent class name.

Now you can see I've got the same classes with the same properties as before. However, now I was able to do this by "sharing" or inheriting those properties from a parent class rather than repeating myself for each class.

Figure 3.

This also opens up the opportunity for adding more vehicles as well such as vans, if I wish rather than copying and pasting a similar class and adding a few specific properties.

As you can see, defining inheritance is relatively easy to do. The hard part is not how to create class inheritance but rather when to create a parent class. As you're creating classes, always have in the back of your mind the concept of DRY. If you find yourself using the same properties, it's probably a good time to start thinking about creating a parent class for those two classes and inheriting the shared properties.

When creating a new class, rather than just building one on a whim, first think about any other classes that you've set up in this project. Are they related in some way to the class you're about to create? If so, before writing any code, always first try to define the class as a child class and only if it does not fit, then create a standalone class. If this method is regularly followed, the code that's generated will be much more manageable.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


Featured

comments powered by Disqus

Subscribe on YouTube