Chapter 13. Inheritance

 <  Day Day Up  >  

Often, types in a program share the same characteristics. For example, a program may contain types that represent a customer and an employee.

 Class Customer   Public Name As String   Public Address As String   Public City As String   Public State As String   Public ZIP As String   Public CustomerID As Integer End Class Class Employee   Public Name As String   Public Address As String   Public City As String   Public State As String   Public ZIP As String   Public Salary As Integer End Class 

In this situation, both the Customer and Employee classes contain a number of identical fields. This is because the two classes each describe a person, and a person has certain characteristics, such as a name and address, that exist independent of whether or not they are a customer or an employee.

This commonality between the Customer type and the Employee type can be expressed through inheritance . Instead of repeating the same information in both types, you can create a class called Person that contains the common characteristics of a person.

 Class Person   Public Name As String   Public Address As String   Public City As String   Public State As String   Public ZIP As String End Class 

The class Person represents all the characteristics of a person that exist independent of whether the person is a customer or an employee. Once the Person class is defined, the Customer and Employee classes can inherit all the members of the Person class. This means that the classes have to define only the members that are unique to each class.

 Class Customer   Inherits Person   Public CustomerID As Integer End Class Class Employee   Inherits Person   Public Salary As Integer End Class 

When one class inherits members from another class, the inheriting class derives from the other type. The type being derived from is called the base type . A type inherits all the members that the base type defines, including methods and events. So the Employee and Customer classes still have fields named Name , Address , City , State , ZIP , and Phone , even though they don't explicitly declare them, because they inherit them from Person . For example, the classes can be used as follows .

 Module Test   Sub Main()     Dim c As Customer = New Customer()     c.Name = "John Smith"     Dim e As Employee = New Employee()     e.Name = "Jane Doe"   End Sub End Module 

Advanced

Visual Basic .NET supports only single inheritance , which means that a class can derive from only one base type.


A class that derives from another class can in turn be derived from by another class. For example, Employee can be further specialized by classes such as Manager and Programmer .

 Class Programmer   Inherits Employee   Public Project As String End Class Class Manager   Inherits Employee   Public Programmers() As Programmer End Class 

In this example, the Programmer class contains the members defined in its immediate base class, Employee , as well the members defined in Employee 's base class, Person . Related types can be viewed as a hierarchy with a tree structure, as in Figure 13-1.

Figure 13-1. An Inheritance Hierarchy

graphics/13fig01.gif

Obviously, a type cannot directly or indirectly inherit from itself. Also, notice that the type Object is at the top of the inheritance hierarchy. If a class does not explicitly inherit from another class, it inherits from Object by default. Thus, Object is always the common root of all inheritance hierarchies. Also notice that in this type hierarchy, the most general types are at the top of the tree. As you move down the hierarchy, the classes at each level become more specialized and specific. Inheritance is a very powerful way of expressing the relationships between types.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net