Home > Articles

Inheritance in VB.NET

  • Print
  • + Share This
  • 💬 Discuss
Inheritance is a powerful way of expressing the relationships between types and reusing code across multiple types. Paul Vick focuses on how to build a hierarchy of related classes in VB.NET and to reuse functionality defined in existing classes.

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.

13fig01.gifFigure 13-1. An Inheritance Hierarchy

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.

Protected Accessibility

An important thing to keep in mind about inheritance and accessibility is that a derived class does not have access to its base classes' Private members. Private members can be accessed only by the immediate type in which they're defined. Protected members, however, can be accessed within an inheritance hierarchy. The Protected access level restricts access to a member to only the class itself, but it extends access to all derived classes as well. For example:

Class User
  Private SSN As String
  Protected Password As String
End Class

Class Guest
  Inherits User

  Sub New()
    ' Error: SSN is private to User
    SSN = "123-45-7890"

    ' OK: Password is protected and can be accessed
    Password = "password"
  End Sub
End Class

The class Guest can access the Password field inherited from its base class because it is Protected. However, it cannot access the SSN field, because it is Private.

When a class accesses a Protected member, the access must take place through an instance of that class or a more derived class. It cannot take place through a base class. For example, the following code is incorrect.

Class User
  Protected Name As String
  Private SSN As String
  Protected Password As String
End Class

Class Guest
  Inherits User

  Shared Sub ChangeName(ByVal u As User, ByVal Name As String)
    ' Error: Access to Name in User cannot go through
    ' base class User
    u.Name = Name
  End Sub
End Class

This rule may seem strange, but it is necessary to prevent unexpected access to Protected members. Without the rule, it would be possible to gain access to a Protected member of another type simply by deriving from a common base class.

Class User
  Protected Name As String
  Private SSN As String
  Protected Password As String
End Class

Class Administrator
  Inherits User
End Class

Class Guest
  Inherits User

  Public Sub PrintAdministratorPassword(ByVal u As User)
    ' Error: Access to Password in User cannot go through
    ' base class User
    Console.WriteLine(u.Password)
  End Sub
End Class

In this example, Guest cannot access Administrator's protected field Password—it can only access the Password field of instances of Guest.

Protected and Friend access levels can also be combined—the Protected Friend access level is the union of the two access levels.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus