Introduction and Implementing Inheritance
The usefulness of inheritance is depnding on the choice of its usage. You can use inheritance if the derived class is a kind of base class but not has a relationship with the base class. Such a situation empowers you to reuse the code from the base class. It is also more useful if the hierarchy is very shallow. The developer can effect global changed to the derived class by changing the base class.
Understanding Inheritance
Inheritance is an important feature of any OOP Language. Let us examine the following lines of code:
Public Class DemoForm
Inherits System.Windows.Forms.Form
…………
End Class
Note the use of public in the above code. You can use any of the four access specifiers namely Public, Private, Protected, and private, while declaring the classes. This usage depends upon the need of the program to make the other classes capable of inheriting from this class.
Note the use of public in the above code. You can use any of the four access specifiers namely Public, Private, Protected, and private, while declaring the classes. This usage depends upon the need of the program to make the other classes capable of inheriting from this class.In the above code we have created a new class DemoForm based on the class System.Windows.Forms.Form. The new class DemoForm is the derived class and the class Form is the base class.
Some of the points of interest about inheritances are given below:
- In Visual Basic .NET only single inheritance is permitted; that is, any derived class can have only one base class
- You can inherit all classes except those marked with keywork NotInheritable.
- You can restrict certain items in base class from being exposed by using access specifiers. Access type of a derived class must be equal or more restrictive than its base class for the inheritance to succeed.
Inherits keyword is used before the base class to specify the base class. MustInherit modifier is used to specify that the class is intended for use as a base class only.
The derived class inherits all the methods defined in the base class by default. You may come across some instances that a particular method needs to behave differently in the derived class. In such a case, the method can be overridden in the derived class. In this case you have to specify a new implementation of the method in the derived class. This is possible only if the method in the base class is marked with keywork overridable.
The method in the derived class should be declared as overrides. Declaring a method as NotOverridable prevents you from overriding the method in a derived class. Public mehtods are NotOverridable by default. You can also declare a method as MustOverride which makes it mandatory for you to override the method in a derived class. Any method that is declared as MustInherit should not contain any body in the method statement.
You can refer to any method in the base class by using the keyword MyBase as illustrated in the code fragment given below.
Public Class BaseClass1
Public Overridable Sub someMethod()
‘… your code
End Sub
End Class
Class DerivedClass1
Inherits BaseClass1
Public Overrides Sub someMethod()
‘The codes for the Sub …
End Sub
Public Sub baseClassMethod()
MyBase.someMethod()
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DerivedClass As New DerivedClass1
DerivedClass.someMethod()
DerivedClass.baseClassMethod()
End Sub
End Class
In the above illustration calling the sub someMethod calls the overridden method in the derived class. However the statement MyBase.someMethod calls the Sub defined in the base class. MyBase keyword can be used to access the immediate base class and its inherited members. Private members of the base class cannot be accessed using MyBase keyword.
It is not an object and therefore cannot be assigned to variables or passed on procedure or used for Is comparison. However MyBase keyword cannot be used to access a Sub declared as MustOverride in the base class.MyBase cannot be used in modules and to access base class members that are marked as friend if the base class is in a different assembly.
MyClass keyword
This keyword allows you to call an overridable method implemented in your clas and make sure that implementation of the method in the specified class is called instead of the overridden method in the derived class.Like MyBase MyClass is also a keyword and not a class. MyClass keyword can be used to refer the containing class and its inherited members.
MyClass can be used to qualify the shared members and it cannot be used in standard modules. In case the method has no implementation in the derived class MyClass keyword can be used to refer the method in the base class and therefore the effect of such usage is the same as the use of MyBase.
Implementing Inhertance
Class BaseClass1
Sub Method1()
MessageBox.Show("This is a method in the base class.")
End Sub
Overridable Sub Method2()
MessageBox.Show("This is another method in the base class.")
End Sub
End Class
Class DerivedClass2
Inherits BaseClass1
Public Field2 As Integer
Overrides Sub Method2()
Messagebox.Show("This is a method in a derived class.")
End Sub
Protected Sub TestInheritance()
Dim C1 As New BaseClass1()
Dim C2 As New DerivedClass2()
C1.Method1() ‘ Calls a method in the base class.
C1.Method2() ‘ Calls another method from the base class.
C2.Method1() ‘ Calls an inherited method from the base class.
C2.Method2() ‘ Calls a method from the derived class.
End Sub
End Class
When you run the procedure TestInheritance, you see the following messages:
"This is a method in the base class."
"This is another method in the base class."
"This is a method in the base class."
"This is a method in a derived class."
When to use inhertance
The usefulness of inheritance is depnding on the choice of its usage. You can use inheritance if the derived class is a kind of base class but not has a relationship with the base class. Such a situation empowers you to reuse the code from the base class. It is also more useful if the hierarchy is very shallow. The developer can effect global changed to the derived class by changing the base class.
Inheritance and .NET Framework
In .NET Framework the inheritance is implemented with additional functionality. Any class created in any of the programing language in the Framework can be inherited in another language. Thus cross language inheritance is also implemented in this Framework.
The System.Object class serves as a common base class for all objects in the .NET framework, that ensures interoperability of objects developed using Visual Studio .NET. New classes implicitly inherit the System.Object class; therefore it is never necessary to explicitly name this class with an Inherits statement. Among the methods that objects inherit from System.Object, one of the most useful is Object.GetType, which is used to return the exact type of the current object.