Using Component (Inheritance and Multinheritance) in ASP.NET Part 8
Inheritance and
Multinheritance
In very rough language, inheritance is a practice of coding where we pass property of any class to any other class. The main key feature of such work is reusability. It is all time saving and useful if we can reuse something that already exists rather than typing once more. This is done by creating a new class from an existing class. The process of deriving a new class from an existing class is called Inheritance. The old class is called the 'base class' and the new class is called 'derived class'.
Public Class BaseProduct
Private _price As Decimal
In very rough language, inheritance is a practice of coding where we pass property of any class to any other class. The main key feature of such work is reusability. It is all time saving and useful if we can reuse something that already exists rather than typing once more. This is done by creating a new class from an existing class. The process of deriving a new class from an existing class is called Inheritance. The old class is called the 'base class' and the new class is called 'derived class'.
Public Class BaseProduct
Private _price As Decimal
Public Property Price() As Decimal
Get
Return _price
End Get
Set(ByVal Value As Decimal)
_price = Value
End Set
End Property
Get
Return _price
End Get
Set(ByVal Value As Decimal)
_price = Value
End Set
End Property
End Class
Public Class ComputerProduct
Inherits BaseProduct
Inherits BaseProduct
Private _processor As String
Public Property Processor() As String
Get
Return _processor
End Get
Set(ByVal Value As String)
_processor = value
End Set
End Property
Get
Return _processor
End Get
Set(ByVal Value As String)
_processor = value
End Set
End Property
End Class
Public Class TelevisionProduct
Inherits BaseProduct
Inherits BaseProduct
Private _isHDTV As Boolean
Public Property IsHDTV() As Boolean
Get
Return _isHDTV
End Get
Set(ByVal Value As Boolean)
_isHDTV = value
End Set
End Property
End Class
Notice that both the ComputerProduct and TelevisionProduct components inherit from the BaseProduct component. Because the BaseProduct class includes a Price property, both inherited components automatically inherit this property.
When inheriting one class from another, we also can override methods and properties of the base class. Overriding a method or property is useful when we want to modify the behavior of an existing class.
To override a property or method of a base class, the property or method must be marked with the Visual Basic .NET Overridable or MustOverride keyword. Only methods or properties marked with the Overridable or MustOverride keyword can be overridden.
Public Class ProductBase
Get
Return _isHDTV
End Get
Set(ByVal Value As Boolean)
_isHDTV = value
End Set
End Property
End Class
Notice that both the ComputerProduct and TelevisionProduct components inherit from the BaseProduct component. Because the BaseProduct class includes a Price property, both inherited components automatically inherit this property.
When inheriting one class from another, we also can override methods and properties of the base class. Overriding a method or property is useful when we want to modify the behavior of an existing class.
To override a property or method of a base class, the property or method must be marked with the Visual Basic .NET Overridable or MustOverride keyword. Only methods or properties marked with the Overridable or MustOverride keyword can be overridden.
Public Class ProductBase
Private _price As Decimal
Public Overridable Property Price() As Decimal
Get
Return _price
End Get
Set(ByVal Value As Decimal)
_price = value
End Set
End Property
Get
Return _price
End Get
Set(ByVal Value As Decimal)
_price = value
End Set
End Property
End Class
Public Class OnSaleProduct
Inherits ProductBase
Inherits ProductBase
Public Overrides Property Price() As Decimal
Get
Return MyBase.Price / 2
End Get
Set(ByVal Value As Decimal)
MyBase.Price = value
End Set
End Property
Get
Return MyBase.Price / 2
End Get
Set(ByVal Value As Decimal)
MyBase.Price = value
End Set
End Property
End Class
Notice that the MyBase keyword is used in above example to refer to the base class (the ProductBase class).
Finally, we can use the MustInherit keyword when declaring a class to mark the class as an abstract class. We cannot instantiate a MustInherit class. To use a MustInherit class, we must derive a new class from the MustInherit class and instantiate the derived class.
MustInherit classes are the foundation for the ASP.NET 2.0 Provider Model. Personalization, Membership, Roles, Session State, and Site Maps all use the Provider Model.
For example, the MembershipProvider class is the base class for all Membership Providers. The SqlMembershipProvider and ActiveDirectoryMembershipProvider classes both derive from the base MembershipProvider class.
The base MembershipProvider class is a MustInherit class. We cannot use this class directly in your code. Instead, we must use one of its derived classes. However, the base MembershipProvider class provides a common set of methods and properties that all MembershipProvider-derived classes inherit.
The base MembershipProvider class includes a number of methods and properties marked as MustOverride. A derived MembershipProvider class is required to override these properties and methods.
The file in gven below contains two components. The first component, the BaseEmployee component, is a MustInherit class that contains a MustOverride property named Salary. The second component, the SalesEmployee, inherits the BaseEmployee component and overrides the Salary property.
Public MustInherit Class BaseEmployee
Notice that the MyBase keyword is used in above example to refer to the base class (the ProductBase class).
Finally, we can use the MustInherit keyword when declaring a class to mark the class as an abstract class. We cannot instantiate a MustInherit class. To use a MustInherit class, we must derive a new class from the MustInherit class and instantiate the derived class.
MustInherit classes are the foundation for the ASP.NET 2.0 Provider Model. Personalization, Membership, Roles, Session State, and Site Maps all use the Provider Model.
For example, the MembershipProvider class is the base class for all Membership Providers. The SqlMembershipProvider and ActiveDirectoryMembershipProvider classes both derive from the base MembershipProvider class.
The base MembershipProvider class is a MustInherit class. We cannot use this class directly in your code. Instead, we must use one of its derived classes. However, the base MembershipProvider class provides a common set of methods and properties that all MembershipProvider-derived classes inherit.
The base MembershipProvider class includes a number of methods and properties marked as MustOverride. A derived MembershipProvider class is required to override these properties and methods.
The file in gven below contains two components. The first component, the BaseEmployee component, is a MustInherit class that contains a MustOverride property named Salary. The second component, the SalesEmployee, inherits the BaseEmployee component and overrides the Salary property.
Public MustInherit Class BaseEmployee
Public MustOverride ReadOnly Property Salary() As Decimal
Public ReadOnly Property Company() As String
Get
Return "MINDCRACKER"
End Get
End Property
Get
Return "MINDCRACKER"
End Get
End Property
End Class
Public Class SalesEmployee
Inherits BaseEmployee
Inherits BaseEmployee
Public Overrides ReadOnly Property Salary() As Decimal
Get
Return 67000.23D
End Get
End Property
Get
Return 67000.23D
End Get
End Property
End Class
Note: Continue in Next Part.
Note: Continue in Next Part.
Comments
Post a Comment