Inheritance

Using inheritance, you can create a class that is based on an existing class, giving your new class all the behavior and functionality of the existing class. The class that you inherit from is known as the base class, and the class that is inheriting the functionality is known as the derived class. You can extend the functionality of the derived class by adding properties or methods that did not exist in the base class, or you can override inherited properties or methods so that they behave differently in the derived class.

Visual Basic 2005 supports inheritance, although it doesn't support multiple inheritance. A derived class can have only one base class.

Using inheritance, you can reuse existing code that performs most of the functionality you require. You modify only a portion of the code to meet your needs, instead of having to reinvent the wheel. Whenever you need functionality in your application, you should look at the .NET Framework class library to see whether the functionality exists or whether you can inherit the base functionality from one of the classes. For example, if you want to extend an existing Textbox control, you can create a new class that derives from the Windows Forms Textbox control, as shown in Listing 3.12.

Listing 3.12. Inheriting from an existing Windows Forms control Public Class MyTextBox

Inherits System.Windows.Forms.TextBox

' Add code to override existing TextBox functionality. End Class Polymorphism

Polymorphism is the ability to create identically named methods or properties within a number of derived classes that perform different functions. You can implement polymorphism by using interfaces or inheritance. For inheritance-based polymorphism, you override methods in a base class with new implementations of the methods in the derived class. For interface-based polymorphism, you implement an interface differently in multiple classes. You saw an example of this when you created multiple constructors for your Sentence class.

0 0

Post a comment