Chapter 25. Using Inheritance and Polymorphism

Subroutines and functions enable you to reuse code by packaging it so other pieces of code can call it easily. By using inheritance, you can reuse code in another way: by making one class reuse the properties, methods, and events of another class.

In this lesson you learn how to use inheritance to make one class reuse the code of another. You also learn how polymorphism enables a program to treat an object of one class as if it belonged to another.

INHERITANCE

One way to understand inheritance is to realize that real-world things of one type may also be of another type. For example, an employee is also a person (despite what some managers may think). That means whatever attributes a person has, an employee has also. A person has a name, address, birthday, and so forth. An employee also has those things, in addition to some new ones such as a salary, job title, office number, and employee ID number.

Similarly, a manager is a type of employee. That means a manager has everything an employee does, which in turn means the manager has everything a person does.

In Visual Basic, classes provide a similar structure through inheritance. One class such as Employee can inherit the properties, methods, and events of another class such as Person.

This kind of inheritance enables you to reuse code in a new way because it means you don't need to write the same properties, methods, and events for the Employee class that you already defined for the Person ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer 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.