Inheritance & Delegation Concepts

Inheritance and Delegation are Object Oriented Programming (OOP) Concepts that are utilized by the Creational Design Pattern category which gets its name from handling the creation of objects or instantiation of classes. Class patterns use inheritance whereas object creation patterns use delegation.

Inheritance

The OOP concept of Inheritance allows us to create base classes with existing methods, that can be utilized or overridden in the calling class.

When creating a new user control, ASP.NET normally sets it to inherit the System.Web.UI.UserControl. However, we want to add additional code and override certain methods of the System.Web.UI.UserControl, so we created a new Public Class ShiningStarControl, which allows us to do that. In this example, our ShiningStarControl Inherits the System.Web.UI.UserControl and allows us to add our additional GetTheme Function, as well as create numerous other unique functions.


Public Class ShiningStarControl
    Inherits System.Web.UI.UserControl

    Public Function GetTheme() As String

        Return ShiningStarPage.GetTheme()

    End Function
...

Delegation

Object Creation patterns use Delegation to Invoke methods used in differing objects. In the below example, in VB.NET, a delegate constructor SaveDelegate is defined to be a Function that returns a Boolean. The user can call a specific Save function by utilizing the "AddressOf" operator. The CheckSave function retrieves the parameter as a SaveDelegate Delegate. And within our CheckSave function, the actual Save method is called with a MethodName.Invoke(). Our CheckSave function can now issue further functionality that is shared by all the save functions.


Delegate Function SaveDelegate() As Boolean
....
Dim returnVal As String = CheckSave(AddressOf SaveCheckPhone)

Dim returnVal As String = CheckSave(AddressOf SaveCheckLanguage)
...                                    

Public Function CheckSave(ByVal MethodName As SaveDelegate) As String

Dim bSuccess As Boolean = MethodName.Invoke() 
...