Chapter 18. Object-Oriented Visual Basic 2005
A true object-oriented language supports the following features, all found in Visual Basic 2005: programmer-defined types, encapsulation, specialization, and polymorphism. This chapter will briefly review how these hallmarks of object-oriented programming are implemented in Visual Basic 2005.
Chapter 15 discussed the myriad primitive types built into the Visual Basic 2005 language, such as Integer
, Long
, and Single
. The heart and soul of Visual Basic 2005, however, is the ability to create programmer-defined types.
You specify new types in Visual Basic 2005 by declaring and defining classes. Instances of a class are called objects. The difference between a class and an object is the same as the difference between the concept of a Dog and the particular dog who is sitting at your feet as you read this. You can't play fetch with the definition of a Dog, only with an instance (unless your instance is lazy and would prefer to sleep on the sofa that you just vacuumed thank you very much, and whose only interest is in eating and going out in the snow and then you have to clean his feet and, well, just don't get me started, okay?)
A Dog class describes what dogs are like: they have weight, height, eye color, hair color, disposition, and so forth. They also have actions they can take, such as eat, walk, bark, and sleep. A particular dog (such as my dog, Milo) will have a specific weight (62 pounds), height (22 inches), eye color (black), hair ...
Get Programming Visual Basic 2005 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.