Chapter 4. Object-Orientation

Several different mechanisms of reuse are available in Visual Basic. Implementation inheritance allows you to derive a child class from a parent class. The behaviors of the parent class are inherited by the child and possibly extended. The parent class typically contains general operations (if any), while the child class contains more specialized behaviors. The relationship is known as an is-a relationship (i.e., a Car is a Vehicle).

The second type of reuse, which is often confusing to beginning programmers, is containment. In containment, one class contains another, representing a has-a relationship. For instance, a Vehicle has a Motor.

Finally, in Chapter 5, you will learn about interface-based programming. Interfaces represent services that don't necessarily conform to class boundaries. The relationship could be described as a provides-a relationship. A Vehicle, for example, can be painted, but so can a House. These two objects are completely different, unrelated in almost every way. Although painting either object would involve vastly different work, a common description of the process (characterized by a common verb, such as "to paint") is possible.

While the syntax of each type of reuse is simple, the when and the why require more of an explanation. For instance, not all relationships defined in terms of is-a are appropriate for inheritance. Improper modeling of these relationships accounts for many of the design errors in an object-oriented ...

Get Object-Oriented Programming with Visual Basic .NET 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.