Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   Tutors' Corner (http://www.xtremevbtalk.com/tutors-corner/)
-   -   Basic Inheritance in VB.Net (http://www.xtremevbtalk.com/tutors-corner/156435-basic-inheritance-vb-net.html)

excaliber 03-26-2004 05:43 PM

Inheritance is a big part of Object Oriented Programming (OOP). It allows classes to specify a 'super-class', allowing property and method reuse in an intelligent manner. Inheritance can usually be described using a good analogy.

A Scenario
Lets assume you are building some software for a warehouse. The warehouse sells all kinds of musical products, such as a guitars and drums. We see three objects here. There are products, drums and guitars. We can also see that both drums and guitars are both products, so by default probably share some similarities.

For instance, both a guitar and a drum would have a CheckInventory() method, to see if it's in stock. They would also both probably have a brand name property, and a serial number. The drum would have a special method for shipping (as it requires special preparation to ship), and the guitar would have a special property for what type of guitar it was (acoustic or electric).

Non-Inheritance Method
If you were to forego inheritance, you would need to create two classes, a drum class and a guitar class. Each class would have to include code for CheckInventory, name property, serial property, and any special methods/properties. Assume you have hundreds of products, each with special requirements that make them unique. Thats alot of code to bang out, and alot of redundancy.

Inheritance Method
Inheritance solves this. There are two types of inheritance: interface and implementation. We are going to look at implementation today. Lets look at the above scenario, and see how inheritance makes life alot easier.

Base Class
To start, we need to define a base class (also sometimes referred to as a super-class). The base class contains all code that is common to all 'derived' classes (classes that use the base class). In this case, the drum and guitar are both products. So we will create a product base class. The product class will contain the CheckInventory method, the name property, and the serial number property. It will hold these as all products share these features, and thus the same code.

Fairly simple code. We've got a class that contains a method, and two properties that expose two datastructures.

Public Class Product Private strName as string Private strSerial as string Public Function CheckInventory() as Boolean 'Code for checking inventory status here End Function Public Property Name() as String Get Name() = strName End Get Set(ByVal Value as String) strName = Value End Set End Property Public Property Serial() as String Get Serial() = strSerial End Get Set(ByVal Value as String) strSerial = Value End Set End Property

Guitar Class (Derived Class)
Now for the inheritance portion. The drum has a special method for packaging, while the guitar has a special property for the type. Heres the code for the Guitar class:

Public Class Guitar 'Inherit the methods/properties of the Product class Inherits Product Private strType as String 'Additional property to add to the existing properties/methods of Product Public Property Type() as String Get Type() = strType End Get Set(ByVal Value as String) strType = Value End Set End Property End Class

Notice the "Inherits Product" line. This forces the class to inherit the properties and methods of Product, as well as exposing the methods and properties of the Guitar class. Here is some sample code for the Drum class:

Drum Class (Derived Class)

Public Class Drum Inherits Product Public Function Ship() 'Shipping code here End Function End Class

Just as simple, eh? Here is some code to utilize the three above classes:

Using the Guitar Class
Dim name As String Dim serial As String Dim type As String 'Notice how Order() is declared as a product, and 'not as a Guitar or Drum class. Dim Order() As Product 'We redimension and set the new member to a Guitar class redim Order(0) Order(0) = new Guitar 'Utilizes the properties of the base Product class name = Order(0).Name serial = Order(0).Serial 'Utilizes the property from the derived class, Guitar 'The type conversion is needed to tell the compiler he 'shouldn't worry: the Product object does indeed have 'the Type property, because it's a Guitar type = CType(Order(0), Guitar).Type If Order(0).CheckInventory = True Then MsgBox("In Stock") End if 'Now lets do the same for the Drum class redim Preserve Order(UBound(Order)+1) 'cache the UBound for convience Dim upperB as integer UpperB = UBound(Order) 'We redimension and set the new member to a Drum class Order(UpperB) = new Drum 'Utilizes the properties of the base Product class name = Order(UpperB).Name serial = Order(UpperB).Serial If Order(UpperB).CheckInventory = True Then MsgBox("In Stock") End if CType(Order(upperB), Drum).Ship()

Lets look at a couple of things. First, notice how the original Order() variable was declared as a Product class? Because it was declared as a Product, it can be set to any class that Inherits the product class. A parent class can always handle any child class. If we would have declared Order() as a Guitar, it could only handle Guitar classes. By declaring as Product, this gives us better flexibility. This enables us to declare the object, then set it later to what we want. Lets also look at all the methods and properties that each class Inherits from Product.


 Name (Property from Product)
 Serial (Property from Product)
 InventoryCheck (Method from Product)
 -Type (Property from the Guitar class)
 Name (Property from Product)
 Serial (Property from Product)
 InventoryCheck (Method from Product)
 -Ship (Method from the Drum Class)

Overriding Methods and Properties
The next small section will talk about Overriding. Overriding allows you to use a new method/property in place of an old one. Using our analogy, lets say the Drum class needs a different InventoryCheck method. The manufacturer wants to keep track of the number of times the method has been called (to see how often people check on it). This means that it will have to use a different method in place of the old CheckInventory method inherited from Product. We could create an entirely new method with a new name, but thats wasteful and confusing. Instead, we need to go back and edit the CheckInventory method in Product to add one keyword:

Public Overridable Function CheckInventory() as Boolean 'Code for checking inventory status here End Function

Notice the keyword "Overridable"? By placing that in the Product's version of the method, it allows us to "Override", or use our own, method in other classes. Here is how to override it in the Drum Class:

Public Overrides Function CheckInventory() as Boolean 'old code to check inventory '- 'New code to 'track' the number of times called End Function

This time we use a new keyword, "Overrides". This means that this function takes the place of the function named the same in the parent class. When CheckInventory is called, this function will be used instead of the one from Product. These two keywords can be used on anything, from functions to subs to properties. But if you look at it, its still wasteful. We are repeating the original code in the new function. Theres a way around this. Since ultimately Product is a subclass of the System.Object class, it can use MyBase to interact with its base class and use its methods. Heres how it can help:

Public Overrides Function CheckInventory() As Boolean Dim result As Boolean 'Call our base's version of CheckInventory result = MyBase.CheckInventory 'New code to 'track' the number of times called End Function

In that example, we call Product's original CheckInventory method, then proceed to add to it using new code. This is efficient and allows us to utilize old code while customizing it for the Drum class. Note that his may only be helpful in certain situations. There may (and will) be times when you need a completely different version of the method. This is just a little trick to help if it fits your situation.

This tutorial demonstrated how a Product class could contain methods that are shared for all products. A Drum and Guitar class were made that used the methods in Product, but also added their own, without waste. We also modified overrode the default CheckInventory method with one of our own in the Drum class, and used MyBase to keep waste to a minimum. Pretty cool, eh?

All times are GMT -6. The time now is 11:37 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.