- The Class Hierarchy
- Creating a New Class
- Declaration and Instantiation
- Constructors and Destructors
- Garbage Collection
- Inheritance
- Object Operators
- Adding and Overriding Methods
- Calling the Overridden Method
- Overloading
- Casting
- Oddities
- Encapsulation
- Access Scope: Public, Private, Protected
- Setting Properties with Methods
- Default and Optional Parameters
- Declaring Variables Static and Const
- Revisiting the StringParser Module
- Example: Creating a Properties Class
- Data-Oriented Classes and Visual Basic Data Types
- Advanced Techniques
Adding and Overriding Methods
So far, I've created Beta, a subclass of Alpha, but that is all. Except for the type, there's really no difference between them. If you wanted to, you could add new methods to Beta that would be available only to Beta and not Alpha. This is easy enough to do—just add methods to Beta as you would to any class or module.
A more interesting thing you may consider doing is overriding an existing method.
Again, there is a shade of resemblance between object-oriented inheritance and human inheritance. Once in a while (as children are wont to do), the child decides to do something that the parent already does, but in a different way. For example, my father and I both go to church, but he goes to a Baptist church and I go to a Catholic one. In object-oriented circles, this is called overriding.
The way to override one of Alpha's methods in Beta is to implement a method with the exact (and I mean exact) signature as a method that is found in Alpha. Because Alpha implements only one method, getName() (well, two if you count the Constructor, which can also be overridden), we'll override getName() in Beta.
Function getName() as String Return "Beta says: " + me.Name End Function
Now let's see how this works in practice.
Dim a as Alpha Dim b as Beta Dim s,t as String a = New Alpha("Golly!") b = New Beta("Golly!") s = a.getName() // s = "Golly!" t = b.getName() // b = "Beta says: Golly!"