A consequence of inheritance is another powerful OOP technique: polymorphism, which is the capability of a base type to adjust itself to accommodate many different derived types. Let’s make it simpler by using some analogies in the English language. Take the word run, for example.
This verb can be used to describe what athletes, cars, or refrigerators do; they all run. In different sentences, the same word takes on different meanings. When you use it with a person, it means going a distance at a fast pace. When you use it with a refrigerator, it means that it’s working. When you use it with a car, it may take on both meanings. So, in a sense the word run is polymorphic (and so are many other English words): Its exact meaning is differentiated by the context.
To apply the same analogy to computers, think of a class that describes a basic object such as a Shape. This class would be very complicated if it had to describe and handle all shapes. It would be incomplete, too, because the moment you released it to the world, you’d come up with a new shape that can’t be described by your class. To design a class that describes all shapes, you build a simple class to describe shapes at large, and then you build a separate class for each individual shape: a Triangle class, a Square class, a Circle class, and so on. As you can guess, all these classes inherit the Shape class. Let’s also assume that all the classes that describe individual shapes have an Area method, which calculates the area of the shape they describe. The name of the Area method is the same for all classes, but it calculates a different formula for different shapes.
Developers, however, shouldn’t have to learn a different syntax of the Area method for each shape; they can declare a Square object and calculate its area with the following statements:
Dim shape1 As New Square(5)' statements to initialize the square
Dim area As Double = shape1.Area
Code language: PHP (php)
If shape2 represents a circle, the same method will calculate the circle’s area. (I’m assuming that the constructors accept as an argument the square’s side and the circle’s radius, respectively.)
Dim shape2 As New Circle(9.90)
Dim area As Double = shape2.Area
Code language: PHP (php)
You can go through a list of objects derived from the Shape class and calculate their areas by calling the Area method. No need to know what shape each object represents— you just call its Area method. Let’s say you created an ArrayList with various shapes. You can go through the collection and calculate the total area with a loop like the following:
Dim shapeEnum As IEnumerator
Dim totalArea As Double = 0.0
shapeEnum = aList.GetEnumerator
While shapeEnum.MoveNext
totalArea = totalArea + CType(shapeEnum.Current, Shape).Area
End While
Code language: PHP (php)
The CType() function converts the current element of the collection to a Shape object; it’s necessary only if the Strict option is on, which prohibits VB from late-binding the expression. (Strict is off by default.)
One rather obvious alternative is to build a separate function to calculate the area of each shape (SquareArea, CircleArea, and so on). It will work, but why bother with so many function names, not to mention the overhead in your code? You must first figure out the type of shape described by a specific variable, such as shape1, and then call the appropriate method. The code will not be as easy to read, and the longer the application gets, the more If and Case statements you’ll be coding. Not to mention that each method would require different arguments for its calculations.
This approach clearly offsets the benefits of object-oriented programming by reducing classes to collections of functions.
The second, even less-efficient method is a really long Area() function that would be able to calculate the area of all shapes. This function should be a very long Case statement, such as the following one:
Public Function Area(ByVal shapeType As String) As Double
Select Case shapeType
Case "Square": { calculate the area of a square }
Case "Circle": { calculate the area of a circle }
{ . . . more Case statements }
End Select
End Function
Code language: PHP (php)
The real problem with this approach is that every time you want to add a new segment to calculate the area of a new shape to the function, you’d have to edit it. If other developers wanted to add a shape, they’d be out of luck.
In the following section, we’ll build the Shape class, which we’ll extend with individual classes for various shapes. You’ll be able to add your own classes to implement additional shapes, and any code written using the older versions of the Shape class will keep working.