This is the third article in a series focusing on inheritance in VB.NET 2005. In this article, I shall extend my previous article on namespaces to support it with a few more small and intelligent goodies.
The entire source code for this article is available in the form of a downloadable zip. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition. I didn't really test the solution with any other/previous editions. If you have any problems in executing the solution, please post in the discussion area.
Inheriting a class from another namespace
In my previous article, we examined the concept of namespaces in detail. Now, we shall develop classes by inheriting from other classes existing in other namespaces.
Let us examine the following namespace first:
NamespaceNamespace1 PublicClass First Private m_a AsInteger Private m_b AsInteger PublicSubNew(ByVal x AsInteger, ByVal y AsInteger) m_a = x m_b = y EndSub PublicProperty A() AsInteger Get Return m_a EndGet Set(ByVal value AsInteger) m_a = value EndSet EndProperty PublicProperty B() AsInteger Get Return m_b EndGet Set(ByVal value AsInteger) m_b = value EndSet EndProperty EndClass EndNamespace
From the above, you can understand that I defined a class "First" in the namespace "Namespace1." Now, I would like to define another class, "Second," which must inherit the class "First" available in "Namespace1."
This is a continuation from the previous section. Let us walk through the code first:
NamespaceNamespace2 PublicClass Second Inherits Namespace1.First Private m_c AsInteger PublicSubNew(ByVal x AsInteger, ByVal y AsInteger, ByVal z AsInteger) MyBase.New(x, y) m_c = z EndSub PublicProperty C() AsInteger Get Return m_c EndGet Set(ByVal value AsInteger) m_c = value EndSet EndProperty PublicFunction getSum() AsInteger Dim s AsInteger s = Me.A + Me.B + Me.C Return s EndFunction EndClass EndNamespace
From the code above, you can understand that I defined a class "Second" in the namespace "Namespace2." The most important declaration to concentrate on is the following:
PublicClass Second Inherits Namespace1.First
You can observe that the class "Second" (available in "Namespace2") gets inherited from the class "First" available in "Namespace1." If you forgot to specify "Namespace1" in the above scenario, it would have raised an error, because it could never find a class "First" which doesn't belong to any namespace.
To execute the above code, I need to modify the form code as follows:
PublicClass Form1 PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim obj AsNew Namespace2.Second(10, 20, 30) Me.Label1.Text = obj.getSum EndSub EndClass
This is a bit different from what we examined up to now. If I define a class without specifying any namespace, it generally belongs to a root namespace, which is generally the same as the assembly name itself (or the project name).
To put it simply, if you define classes which do not belong to any namespace, they straight away belong to assembly. It is quite easy to inherit from these types of classes, as we don't need to specify any name for the namespace during inheriting.
But there exists a small trick here to learn. We need to understand the "scope" of access. Even though I will not talk about "scope" in a detailed manner, I shall introduce you to a bit of it. In my upcoming articles, I shall explain in detail.
Every project you create will turn into an assembly at run time. That means, when you hit F5 (or run) to execute the project, it converts all of your code in all of your files into a single file called assembly. The physical and logical name of this assembly would be generally the same name as that of our project name (unless we configure it in a different way). To put it simply, an assembly is physical and a namespace is logical (but both will have the same names). An assembly physically stays in the file system (or at references), but we use namespaces (in our programming) to import/access the content of assembly.
To access any class/namespace within an assembly, we need to start with the name of the assembly of import first. All our classes and namespaces are encapsulated within this assembly (logically accessed with the root namespace name). If we wanted to inherit a class from a class within the same assembly and within the same root namespace, generally we don't need to specify the assembly name (or root namespace name). This is what we did (unknowingly) in the first few sections of my previous article.
The next section will give you some practical understanding of the above theory.
This section proceeds based on the theory explained in the previous section. Let us start from the beginning again. In this scenario, the name of the project (or assembly) is "SampleInheritance." Let us define a class (without any namespace) as follows:
PublicClass First . . . EndClass
In the case above, without your understanding, it would be treated as something like the following (internally):
Now, if you wanted to define a new class by inheriting from the above, you can define it in any of the ways as follows:
PublicClass Second Inherits First . . . EndClass PublicClass Second Inherits SampleInheritance.First . . . EndClass
This type of usage or naming would be essential when you have same class names in root namespace and in your own created namespace. To make it simple, if you want to inherit from a class which doesn't belong to any namespace, kindly include the name of the assembly (or project name) to eliminate confusion and to attain better readability.
Finally, if you defined your own namespace as follows:
NamespaceNamespace2 . . . EndNamespace
it would also be treated something like the following (internally):
This section is based on the explanation in the previous two sections. Let us create a solution with the name "SampleInheritance" and add a simple class "First" as follows:
PublicClass First Private m_x AsInteger Private m_y AsInteger PublicSubNew(ByVal a AsInteger, ByVal b AsInteger) m_x = a m_y = b EndSub PublicProperty X() AsInteger Get Return m_x EndGet Set(ByVal value AsInteger) m_x = value EndSet EndProperty PublicProperty Y() AsInteger Get Return m_y EndGet Set(ByVal value AsInteger) m_y = value EndSet EndProperty EndClass
Please be aware that the above doesn't belong to any namespace; instead, it belongs to the root namespace (or the assembly). Let us add one more class "First" as follows:
NamespaceNamespace1 PublicClass First Private m_a AsInteger Private m_b AsInteger PublicSubNew(ByVal x AsInteger, ByVal y AsInteger) m_a = x m_b = y EndSub PublicProperty A() AsInteger Get Return m_a EndGet Set(ByVal value AsInteger) m_a = value EndSet EndProperty PublicProperty B() AsInteger Get Return m_b EndGet Set(ByVal value AsInteger) m_b = value EndSet EndProperty EndClass EndNamespace
The above is another class with the same name "First" with a different set of properties. Observe that it is encapsulated within a new namespace, "Namespace1." The next section will show you how to inherit each of them according to your needs.
From the previous section, we have two classes with the same name "First" (but in different namespaces). Let us create one more class, "Second," as follows:
NamespaceNamespace1 PublicClass Second Inherits First Private m_c AsInteger PublicSubNew(ByVal x AsInteger, ByVal y AsInteger, ByVal z AsInteger) MyBase.New(x, y) m_c = z EndSub PublicProperty C() AsInteger Get Return m_c EndGet Set(ByVal value AsInteger) m_c = value EndSet EndProperty PublicFunction getSum() AsInteger Dim s AsInteger s = Me.A + Me.B + Me.C Return s EndFunction EndClass EndNamespace
The above section is the trickiest part. The class "Second" gets inherited from the class "First." Which "First"? Is it related to the root namespace or the namespace "Namespace1"?
First of all, the class "Second" belongs to the namespace "Namespace1." When this class gets inherited from another class, it checks whether that class exists within its own parent scope or not (in this case, it would be "Namespace1"). If it exists, it inherits from the same. This is called scope. So the above class "Second" gets inherited from the class "First" available in the namespace "Namespace1."
If we need to inherit the class "Second" from the class "First" of the root namespace, we may need to modify the code as follows:
NamespaceNamespace1 PublicClass Second Inherits SampleInheritance.First . . . EndClass EndNamespace
In this article, I simply wanted to explain the topics of OOPS. The sample codes given in this article are neither the best in performance nor the best in programming methodologies.
Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.