What Is Inheritance?

For years, one of the most requested features from Visual Basic developers has been inheritance. Microsoft has had one form of inheritance, interface inheritance, available in VB since VB first started supporting object creation in VB4. Developers, however, wanted to have what is known as implementation inheritance. With VB .NET, developers finally get their wish, thanks to the CLR. If you're unclear on what interface and implementation inheritance are, read on.

Inheritance is a very powerful form of code reuse. With inheritance, you write one class that contains some properties and methods. That class becomes the basis for other classes, and is known as the base class. Classes that then use this base class are known as derived classes. Most often, the derived classes extend the functionality of the base class.

Imagine if you wanted to create an application to model part of a university. As part of your application, you wanted to create two objects, representing students and professors. Students and professors both have a number of things in common: Each has a name, an address, and so forth. However, each also has some differences. Students have declared majors, whereas professors have classes they can teach. Students are charged tuition, whereas professors are paid for their work. Professors submit grades, whereas students receive them.

As you can see, there are similarities and differences that must be taken into account when these objects are built. The goal of inheritance is to write the code for the similarities once, and then use it in both the Student and Professor objects.

Interface Inheritance in VB6

In VB6, you had what was called interface inheritance. With interface inheritance, you could create the interface (or shell, or definition) of a reusable component. Suppose that you wanted to create a Person object that had all the shared attributes of both students and professors. In the case of VB6, you would create a class module that contained the definitions of those attributes, but no actual implementation code. Your interface class might be named iPerson and look like this:

Public Property Get Name() As String
End Property
Public Property Let Name(psName As String)
End Property

Public Property Get Address() As String
End Property
Public Property Let Address(psAddress As String)
End Property

Public Function Enroll() As Boolean
End Function

In VB6, you could create an interface using a class module. Technically, VB could not create interfaces, but you could use the hack of creating a class module with only definitions for properties and methods, and then implement it in another class. This was actually just creating a class, but there would be no implementation code in it, so it “looked like” an interface. For example, the Enroll method does not have any code to actually handle the enrollment. And if you are curious why there is an Enroll function in the iPerson interface, it is because professors can also take courses at the university, which some do because they can do so for free at most institutions.

To use this interface in other classes in VB6, you had to put this line of code in your class:

Implements iPerson

This line set up your new class to inherit the interface from the iPerson class. In VB6, you could actually put implementation code into the interface class, but it was ignored when you implemented the interface in your derived class.

Imagine that you have used the Implements keyword in a class named Student. This class must now implement every public property, method, and event found in iPerson. Therefore, your code in Student would include code similar to the following:

Public Property Get iPerson_Name() As String
    iPerson_Name = msName
End Property

Public Property Let iPerson_Name(psName As String)
    msName = psName
End Property

Public Property Get iPerson_Address() As String
    iPerson_Address = msAddress
End Property

Public Property Let iPerson_Address(psAddress As String)
    msAddress = psAddress
End Property

Public Function iPerson_Enroll() As Boolean
    'open database
    'check to see if class is filled
    'if not, add student
End Function

The code in Student contains the implementation of the interface designed in iPerson.

Looking at this, you'll probably realize that most of the time, the Student implementation code will end up being the same in a Professor class, at least for these class members. Wouldn't it be easier to create this implementation code in iPerson and then just have that code brought into both Student and Professor? That's what implementation inheritance is all about.

VB .NET's Implementation Inheritance

In VB .NET, you don't have to create a separate interface, although you can. Instead, you can just create a class with implementation code and inherit that in other classes. This means you could add the code for the Name and Address properties and the Enroll method in a Person class. Then, the Professor and Student classes could both inherit from the base class Person. Student and Professor would both have access to the code inside of Person, or they could replace it with their own code if needed.

One thing to realize is that in VB .NET, all classes are inheritable by default. This includes forms, which are just a type of class. This means you can create a new form based on an existing form. You will see an example of inheriting forms later in this chapter.

Even though VB .NET provides true implementation inheritance, interfaces are not gone. In fact, they are greatly improved in VB .NET; together, inheritance and interfaces give you the ability to use polymorphism. This will be examined at the end of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.65.208