Applying Inheritance

Before explaining how inheritance is applied in code, a graphical representation can be useful. Figure 12.1 shows how you can create robust hierarchies of custom objects with inheritance.

Figure 12.1 A graphical representation of a custom framework of objects using inheritance.

image

You derive a class from a base class using the Inherits keyword. For example, consider the following implementation of the Person class that exposes some basic properties:

image

Inherits System.Object

In the .NET Framework development, every class inherits from System.Object. Because of this, there is no need to add an inherits directive each time you implement a custom type, because the Visual Basic compiler will do this for you behind the scenes.

The class also offers a FullName method that returns the concatenation of the two FirstName and LastName properties, providing a simplified and basic validation that here is for demonstration purposes. Now we can design a new class that inherits from Person, and in this scenario Person is the base class. The new class is named Contact and represents a personal contact in our everyday life:

image

Contact is the derived class. It receives all public members from Person (in this example both the FirstName and LastName properties and the FullName method) and provides implementation of custom members. This is a typical application of .NET inheritance, in which one derived class inherits from the base class. The .NET Framework does not enable inheriting from multiple classes. You can create a derived class from only a base class. But there are situations in which multiple levels of inheritance would be required; continuing the example of the Person class, there are several kinds of people that you will meet in your life, such as customers, employees of your company, and personal contacts. All these people will have common properties, such as the first name and the last name; therefore, the Person class can be the base class for each of them, providing a common infrastructure that can then be inherited and customized. But over a person representation, if you consider a customer and an employee, both people will have other common properties, such as a title, a business phone number, and an email address. They will differ in the end because of proper characteristics of their role. For this purpose you can implement intermediate classes that are derived classes from a first base class and base classes for other and more specific ones. For example, you could implement an intermediate infrastructure for both customers and employees. The following code snippet provides a class named BusinessPerson that inherits from Person:

image

This class inherits the FirstName and LastName properties from Person (other than methods such as ToString and other public methods exposed by System.Object) and exposes other common properties for classes with a different scope. For example, both a customer and an employee would need the preceding properties but each of them needs its own properties. Because of this, the BusinessPerson class is the intermediate derived class in the hierarchic framework of inheritance. Now consider the following classes, Customer and Employee:

image

Both classes receive the public properties from BusinessPerson and both implement their custom properties according to the particular person they intend to represent. We can summarize the situation as follows:

Customer exposes the following properties:

FirstName and LastName, provided at a higher level by Person

Email, Title, and BusinessPhone provided by BusinessPerson

CustomerID, CompanyName, Address, and ContactPerson provided by its implementation

Employee exposes the following properties:

FirstName and LastName, provided at a higher level by Person

Email, Title, and BusinessPhone provided by BusinessPerson

EmployeeID, HomePhone, MobilePhone, and HireDate provided by its implementation

Because at a higher level we also exposed a method named FullName, which has public visibility, such a method is also visible from derived classes.

Members’ Scope and Inheritance

Remember that only Public, Protected, Friend, and Protected Friend members can be inherited within derived classes.

When available, you can use derived classes the same way as you would do with any other class, even if you do not know at all that a class derives from another one. The following, simple code demonstrates this:

image

Until now we saw only properties in an inheritance demonstration. Methods are also influenced by inheritance and by interesting features that make them powerful.

Inheritance and Common Language Specification

The Common Language Specification establishes that a CLS-compliant class must inherit only from another CLS-compliant class; otherwise, it will not be CLS-compliant.

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

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