{"id":73421,"date":"2015-09-11T00:01:00","date_gmt":"2015-09-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/11\/overriding-inheritance-in-powershell-5-classes\/"},"modified":"2019-02-18T09:35:17","modified_gmt":"2019-02-18T16:35:17","slug":"overriding-inheritance-in-powershell-5-classes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/overriding-inheritance-in-powershell-5-classes\/","title":{"rendered":"Overriding Inheritance in PowerShell 5 Classes"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about overriding inheritance in Windows PowerShell 5.0 classes.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the things that is pretty cool about Windows PowerShell 5.0 classes, is that I can override the things I inherit from a base class. This offers a lot of flexibility because it means that when I inherit from a class, I can customize my new class based on my needs instead of being stuck with a lot of stuff that does not meet the requirements.<\/p>\n<p>One thing I need to do is to override a couple of the properties that I inherit from my <b>Vehicle<\/b> base class. The reason is that I want to limit the <b>Make<\/b> and <b>Color<\/b> to specific values that I define via a couple of enumerations.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> For more information, refer to <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/adding-enums-to-powershell-5-classes\/\" target=\"_blank\">Adding Enums to PowerShell 5 Classes<\/a>.<\/p>\n<p>My base <b>Vehicle<\/b> class defines the make and the color as a string. This can be any string that I want to type&mdash;there are no limitations. However, cars are only made by specific companies, and they only come in specific colors. The string was appropriate for the base <b>Vehicle<\/b> class because I do not want to have a huge enumeration. For example, if I also inherit the <b>Vehicle<\/b> class and create a <b>Bicycle<\/b> class, I will have different makers than I have for cars. I will also probably have different colors.<\/p>\n<p>But when it comes time to creating my <b>Car<\/b> class, I want to limit it to specific car makers and car colors. Here is the <b>Vehicle<\/b> base class:<\/p>\n<p style=\"margin-left:30px\"><strong>Class Vehicle<\/strong><\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [datetime]$year<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$model<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [string]$make<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$color<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Here is my <b>Car<\/b> class that inherits from the <b>Vehicle<\/b> class:<\/p>\n<p style=\"margin-left:30px\">Class Car : Vehicle<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;[string]$vin<\/p>\n<p style=\"margin-left:30px\">&nbsp;[MakeOfCar]$make<\/p>\n<p style=\"margin-left:30px\">&nbsp;[ColorOfCar]$color<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>And here are the two enumerations I want to use&mdash;the <b>MakeOfCar<\/b> enumeration and the <b>ColorOfCar<\/b> enumeration:<\/p>\n<p style=\"margin-left:30px\">Enum MakeOfCar<\/p>\n<p style=\"margin-left:30px\">{&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Chevy = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Ford = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Olds = 3<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Toyota = 4<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; BMW = 5<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Enum ColorOfCar<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Red = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Blue = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Green = 3<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">Class Vehicle<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [datetime]$year<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$model<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [string]$make<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$color<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Class Car : Vehicle<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;[string]$vin<\/p>\n<p style=\"margin-left:30px\">&nbsp;[MakeOfCar]$make<\/p>\n<p style=\"margin-left:30px\">&nbsp;[ColorOfCar]$color<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Enum MakeOfCar<\/p>\n<p style=\"margin-left:30px\">{&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Chevy = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Ford = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Olds = 3<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Toyota = 4<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; BMW = 5<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Enum ColorOfCar<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Red = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Blue = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Green = 3<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>When I create a new instance of the class, I see the output&mdash;note that now the make and the color are initialized to zero.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-11-15-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-11-15-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>After I create a new instance of the <b>Car<\/b> class, I store it in a variable and populate the properties. This iw shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a = [car]::new()<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a.color = 1 ; $a.make = 2 ; $a.model = &quot;mustang&quot; ; $a.vin = 12345 ; $a.year = &quot;1\/1\/2015&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a<\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp; : 12345<\/p>\n<p style=\"margin-left:30px\">make&nbsp; : Ford<\/p>\n<p style=\"margin-left:30px\">color : Red<\/p>\n<p style=\"margin-left:30px\">year&nbsp; : 1\/1\/2015 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">model : mustang<\/p>\n<p>Notice that the <b>Make<\/b> is now a type of the <b>MakeOfCar<\/b> enumeration, so when I say it is equal to 2, I get Ford. The color is also now a type of the <b>ColorOfCar<\/b> enumeration, so 1 equals Red. The <b>Model<\/b> is still a string, so I type <b>Mustang<\/b>. The <b>Year<\/b> is also an instance of the <b>DateTime<\/b> object, so when I type <b>1\/1\/2015<\/b>, it is converted (cast) to a <b>DateTime<\/b> object. The VIN is also a string.<\/p>\n<p>The year, model, make, and color are all defined in the base <b>Vehicle<\/b> class. The VIN is defined in the <b>Car<\/b> class that inherits from the <b>Vehicle<\/b> class. The make and color are overridden in the <b>Car<\/b> class, from being strings to being the appropriate enumeration.<\/p>\n<p>I can verify all of this by looking at my object via <b>Get-Member<\/b>:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a | Get-Member -MemberType Property<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; TypeName: Car<\/p>\n<p style=\"margin-left:30px\">Name&nbsp; MemberType Definition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;- &nbsp; &nbsp; &nbsp; &nbsp;&#8212;&#8212;&#8212;- &nbsp; &nbsp; &nbsp; &nbsp;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">color Property&nbsp;&nbsp; ColorOfCar color {get;set;}<\/p>\n<p style=\"margin-left:30px\">make&nbsp; Property&nbsp;&nbsp; MakeOfCar make {get;set;}&nbsp;<\/p>\n<p style=\"margin-left:30px\">model Property&nbsp;&nbsp; string model {get;set;}&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp; Property&nbsp;&nbsp; string vin {get;set;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">year&nbsp; Property&nbsp;&nbsp; datetime year {get;set;} &nbsp;<\/p>\n<p>Now you understand how to override inheritance in Windows PowerShell 5.0 classes. Windows PowerShell 5.0 Class Week&nbsp;2 will continue tomorrow when I will present a video recap about overloaded constructors.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about overriding inheritance in Windows PowerShell 5.0 classes. Microsoft Scripting Guy, Ed Wilson, is here. One of the things that is pretty cool about Windows PowerShell 5.0 classes, is that I can override the things I inherit from a base class. This offers a lot of flexibility because [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[615,609,3,4,608,45],"class_list":["post-73421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-classes","tag-powershell-5","tag-scripting-guy","tag-scripting-techniques","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about overriding inheritance in Windows PowerShell 5.0 classes. Microsoft Scripting Guy, Ed Wilson, is here. One of the things that is pretty cool about Windows PowerShell 5.0 classes, is that I can override the things I inherit from a base class. This offers a lot of flexibility because [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=73421"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73421\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=73421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}