Inheritance
In VB.Net you can inherit from any object, including objects fro which you don’t have the source code, because all the plumbing code is provided by the .net framework. The only exception to this rule occurs when the another of the class you want to derive from has marked the class sealed, which means that not other class can inherit from it.
The derived class inherits all the Public and Friends fields, properties, methods and events of the base class. Inheriting a field can be a problem, though, because a derived class becomes dependent on that field and the another of the base class can’t change the implementation of that field. For example, to make it a calculated value without breaking the derived class. For this reason, it’s usually preferable that classes meant to work at base classes should include only PrivateFields. You should always use a Property instead of a field to make piece of data visible outside the class because you can always change the internal implementation of a property without any impact on derived classes.
Using Derived Classes: Inheritance rule states that you can always assign a derived object to a base class variable
OverRides Members in the base class: To override a method it must be prefixed with word “Overridable” in the base class.
Example
Base Class
Overridable Function FuncName ( .. ) As String
End Function
Derived Class:
Overrides Function FuncName ( .. ) As String
:
End Function
VB.Net also supports “NotOverridable” key word, which explicitly states that a method can’t be overridden.
Constructors In Derived Classes: If the base class has “New” and this “New” function has no arguments, then we don’t need to call it explicitly. However, if the New function in base class has one or more argunments then we need to call the New Function explicitly from the derived (child) class.
MyClass: Can be used to make sure that the methods and properties are used from the base class and not from the derived class.
Members Shadowing: A member in the derived class method with shadows keyword hides all the members in the base class with the same name.
For Eample,
Class AAA
Function Func1 ( .. )
:
End Function
End Class
Class BBB
Inherits AAA
Shadows Func1 ( .. )
:
End Function
End Class
More on inheritance: In OO term the parent class is called the base class. The child class is called the inherited class or the derived class. Inheritance can be declared two ways.
(C# Style)
Public Class className : Inherits BaseClassName
..
End Class
Or
Public Class className
Inherits BaseClassName
..
End Class
To make a function vertual in baseclass (meaning overridable) we must declare it as OVERIRDABLE
Public OVerridable Function FunctionName (..) As ..
..
ENd Function
In the derived class we declare it as Overides.
Public Overrides Function FunctionName (..) As ..
..
ENd Function
Casting - We can cast a an objects baseclass to a derived class. For Example:
Dim oMyObject As BaseClass = DerivedClass
But this is not allowed
Dim oMyObject As DerivedClass = BaseClass
Because the Option Explicit ON does not allow casting of base type to a derived type.
To do that we need to cast the object.
Dim oMyObject As DerivedClass = DirectCast ( BaseClass, DerivedClass )
Or
Dim oMyObject As DerivedClass = CType ( BaseClass, DerivedClass )
This kind of relationship is called as IsA relationship. So the DerivedClass IsA BaseClass - for example if the base class is called Product and derived class is books then we
can say Books isa Product.
Add a comment