PowerShell Classes Part 4 — Constructors and Inheritance

Posted on September 20, 2017 by Missy Januszko in PowerShell with

Now that you’re familiar with PowerShell classes, as well as its programming concepts and terminology, it is time to show you some other benefits to using PowerShell classes. In this article, I’m going to talk about constructors and how you can use different constructors to create instances of classes with different members. I’m also going to introduce another new concept for classes called inheritance, along with showing some practical examples.

 

 

Previously on PowerShell classes…

I started out by introducing PowerShell classes and general class programming concepts. Here I explored the similarities of a class to a PowerShell object, for example, classes have members – both properties and objects – just like an object in PowerShell. I also introduced creating a new class named Rock and assigning properties to it. Next, I discussed the use of a construct known as an enum to define a custom type for a property and compared enums to the PowerShell parameter validation attribute ValidateSet. Lastly, I showed how to put the three together. So the Rock class now has properties and methods and how some properties make use of enum types.

Default Constructors

In the previous articles, I have already shown how to use the default constructor to create an instance of a class. But what does “constructor” mean? A constructor is a method named the same name as the class. If no constructors are specified within the class, it automatically gets a default constructor with no parameters that I can use to create an instance of a class, like seen in the previous article.

For example, I created an instance of a Rock by using the following:

This creates an instance of a Rock. I am using the default constructor because I have not yet defined any constructors in my class. The instantiation occurs when I call the new() method with no parameters and I will get an instance of a rock with unassigned or default properties.

Overloading constructors

I can create an overload for the constructor. The overload allows me to set default values for properties right in the new method itself. This spares me one or more lines of code in assigning properties later. If I want to continue using the constructor with no parameters also, I need to explicitly define it in the class.

In the next example, I am creating an overload for a constructor of the Rock class that has one parameter, Size.

What exactly does this mean? It means that if I instantiate a class of Rock with one parameter, that one parameter must be the Size parameter than it must be of type Size. A successful instantiation looks like this:


What happens if I try to give it a different parameter, maybe a color instead? When I do that, I receive an error saying that the string is not contained in the enum “Size”. This is also another good argument for using enums! If I had used a string type for Size, “Red” would have been acceptable.

Adding Additional Overloads

Conversely, what happens if I wanted to assign both Size and Color? With the current constructors I have defined, I would also receive an error trying to create this instance. Notice that it is an error about the overload itself. In this case, it is looking for an overload that accepts two arguments. However, I have not created one that accepts two arguments yet.


Here is the constructor for a Rock class that accepts two arguments:

Now, I can create instances of Rocks with zero, one, or two parameters:


However, for the 2-parameter overload, the parameters must be defined in that exact order – size first, then color. By reversing them, PowerShell will attempt to assign “Red” to Size and I receive the same error that I received in one of the previous examples.

Sponsored

What Is Inheritance?

Classes in PowerShell, just like in other object-oriented programming languages, can be hierarchical. This means that I can build a subclass from a parent class and allow the subclass to inherit the properties and members from the parent class. For example, if I had a parent class named “ConstructionMaterials”, that class would contain some properties, likely some of the same properties of the Rock class. A rock is a specific type of construction material, so it inherits the properties from the ConstructionMaterials class. It can also have its own properties. Likewise, I could create other subclasses from ConstructionMaterials, like Brick and Wood. In the following example, I have taken the Rock class and created a ConstructionMaterials class, minimizing the properties.

Then, I will create a Rock class that is a subclass of ConstructionMaterial using the syntax:

Lastly, I will verify that the Rock class is a subclass of ConstructionMaterial.

Adding Additional Properties to a Subclass

Perhaps, I want the Rock subclass to also have the Luster, Texture, and Pattern properties that I defined in the original rock class. In order to accomplish that, we need to load the enum for the values for Luster into memory by either dot-sourcing it or adding it directly to the class file. Next, we create a new Rock class that contains the additional properties.

If I create an instance of a Rock, it contains the properties of the base class (Color, Shape, Size, and Location). In addition, it also contains the Rock-specific properties (Texture, Luster, and Pattern.)

Sponsored

Practice Makes Perfect (Sense)

I have shown how to use the default constructor to create an instance of a class without defining anything specifically in the class itself. I have also shown adding constructor overloads to explicitly define values within the instance of the class. In addition, I have talked about inheritance and how to create base classes and subclasses. Now, it’s your turn. Look for ways you can incorporate classes into your everyday programming. Or, perhaps you might want to just play around with building a class for fun and to continue building your knowledge. Either way, it’s a great way to become familiar with the programming concepts of classes, which will bleed over into other object-oriented languages as well.

Tagged with , , , ,

Register for this Webinar

Real World Solutions with Virtual Reality
Free Whitepaper Download

VR is being utilized for a surprising number of vital business functions today. In this white paper, you’ll get an overview of VR technology and see how it’s a vital part of many industries today.

Download the free whitepaper!