To demonstrate the power of inheritance, we’ll extend an existing class: the ArrayList class. This class comes with the Framework and is a dynamic array. (See Chapter, “Storing Data in Collections,” for a detailed description of the ArrayList class.) The ArrayList class maintains a list of objects, similar to an array, but it’s dynamic. The class we’ll develop in this section will inherit all the functionality of ArrayList, plus it will expose a custom method we’ll implement here: the EliminateDuplicates method. The project described in this section is the CustomArrayList sample project.
Let’s call the new class myArrayList. The first line in the new class must be the Inherits statement, followed by the name of the class we want to inherit, ArrayList. Start a new project, name it CustomArrayList, and add a new class to it. Name the new class myArrayList:
Class myArrayList
Inherits ArrayList
End Class
Code language: PHP (php)
If you don’t add a single line of code to this class, the myArrayList class will expose exactly the same functionality as the ArrayList class. If you add a public function to the class, it will become a method of the new class, in addition to the methods of ArrayList. Add the code of the EliminateDuplicates() subroutine (see Listing 7.1) to the myArrayList class; this subroutine will become a method of the new class.
Listing 7.1: EliminateDuplicates Method for the ArrayList Class
Public Sub EliminateDuplicates()
Dim i As Integer = 0
Dim delEntries As ArrayList
While i <= MyBase.Count - 2
Dim j As Integer = i + 1
While j <= MyBase.count - 1
If MyBase.Item(i).ToString = MyBase.item(j).ToString Then
MyBase.RemoveAt(j)
End If
j = j + 1
End While
i = i + 1
End While
End Sub
Code language: PHP (php)
The code compares each item with all following items and removes any duplicates. The duplicate items are the ones whose ToString property returns the same value. You might wish to perform specific comparisons, but the ToString method will do for our demo. Notice that the code accesses the members of the ArrayList class through the MyBase keyword. MyBase is a keyword that represents the base class, from which the custom class inherits. To test the derived class, place a button on the test form and insert the code presented by Listing 7.2 in its Click event handler.
Listing 7.2: Testing the EliminateDuplicates Method
Private Sub bttnTest Click(...) Handles bttnTest.Click
Dim mlist As New myArrayList()
mlist.Add("10")
mlist.Add("A")
mlist.Add("20")
mlist.Add("087")
mlist.Add("c")
mlist.Add("A")
mlist.Add("b")
mlist.Add("a")
mlist.Add("A")
mlist.Add("87")
mlist.Add(10)
mlist.Add(100)
mlist.Add(110)
mlist.Add("1001")
Console.WriteLine(mlist.GetString())
mlist.EliminateDuplicates()
Console.WriteLine(mlist.GetString())
End Sub
Code language: CSS (css)
Table 7.1 shows the contents of the ArrayList before and after the elimination of the duplicates. Notice that the second list contains the item 10 twice. One of the items is a string, and the other one is a numeric value; therefore, they’re not duplicates.
Table 7.1: The mList ArrayList before and after the Elimination of Duplicates
Original List | After Elimination of Duplicates |
---|---|
10 | 10 |
A | 10A |
20 | 20 |
087 | 087 |
C | C |
A | B |
B | A |
A | 87 |
A | 10 |
87 | 100 |
10 | 110 |
100 | 1001 |
110 | |
1001 |
GetString (see Listing 7.3) is not a method of the ArrayList; it’s a method of the extended ArrayList class, which returns the values of all the items in the list. (It uses each item’s ToString method to retrieve the string representation of the individual items and concatenates them with a line feed separator.)
Listing 7.3: GetString Method
Function GetString() As String
Dim i As Integer
Dim strValue As String
strValue = MyBase.Item(0).ToString
For i = 1 To MyBase.Count - 1
strValue = strValue & vbCrLf & MyBase.Item(i).ToString
Next
GetString = strValue
End Function
Code language: JavaScript (javascript)
Another problem with the ArrayList class is that it can’t sort its elements if they’re not of the same type. You can always provide a custom comparer for custom types, but it’s impossible to write a comparer that can handle all objects. Sometimes, however, we need to know the smallest or largest numeric element, or the alphabetically first or last element. These methods apply to numeric or string elements only; if some of the collection’s elements are objects, we can ignore them. Let’s implement two more custom methods for the myArrayList class (see Listing 7.4). The Min method returns the alphabetically smallest value; the NumMin method returns the numerically smallest value.
Listing 7.4: Min and NumMin Methods of the ArrayList Class
Function Min() As String
Dim i As Integer
Dim minValue As String
minValue = MyBase.Item(0).ToString
For i = 1 To MyBase.Count - 1
If MyBase.Item(i).ToString < minValue Then _
minValue = MyBase.Item(i).ToString
Next
Min = minValue
End Function
Function NumMin() As Double
Dim i As Integer
Dim minValue As Double
minValue = 1E+230
For i = 1 To MyBase.Count - 1
If IsNumeric(MyBase.item(i)) And _
val(MyBase.Item(i).tostring) < minValue Then _
minValue = val(MyBase.Item(i).tostring)
Next
NumMin = minValue
End Function
Code language: PHP (php)
You can populate the myArrayList collection with strings and integers and call the Min and NumMin methods to retrieve the smaller string or numeric value in the list.
What have we done in this section, really? We took an existing class, a powerful one, and extended it. We did that by writing simple procedures that could have appeared in any application. We just inserted the Inherits keyword followed by the name of an existing class on which we want to base our class, and provided the implementation of the new methods. A few more keywords to learn, and you can practically customize any class that comes with the Framework. Existing applications won’t break (the ArrayList class is actually used by some system services, which will keep working fine); they see the original class, not the customized class. Some of your new applications will see the enhanced ArrayList. Another developer might further extend the functionality of your derived class. The old applications will work because ArrayList is still around, your applications will also work because myArrayList hasn’t been modified, and someone else’s applications will work with another class derived from yours.
This type of inheritance, in which we inherit an existing class and add new members and/or revise existing ones, is called implementation inheritance. Implementation inheritance is a powerful feature and can be used in many situations, besides enhancing an existing class. You can design base classes that address a large category of objects and then subclass them for specific objects. The typical example is the Person class, from which classes such as Contact, Customer, Employee, and so on can be derived. Inheritance is used with large-scale projects to ensure consistent behavior across the application. Later in this chapter, you’ll see an interesting application of inheritance. We’ll build classes that describe related objects (shapes), all of which will be based on a single class that encapsulates the basic characteristics of all derived classes.