Introduction to VB.NET Object-Oriented Features
Pages: 1, 2, 3
Inheritance
In the first example, we have seen how a class can be defined and instantiated. In the next example, we will take a look at how classes can be inherited. Listing 2 shows the class definition of a class called Shape.
Listing 2. Shape Class
Public MustInherit Class Shape
Dim l, b As Single
Public MustOverride Function Area() As Single
Public Overridable Function Perimeter() As Single
Return 2 * (l + b)
End Function
Property length()
Get
Return l
End Get
Set(ByVal Value)
l = Value
End Set
End Property
Property breadth()
Get
Return b
End Get
Set(ByVal Value)
b = Value
End Set
End Property
End Class
The Shape class contains two properties -- length and breadth. It also contains two methods -- Area() and Perimeter(). Let's take a more detailed look at the Shape class now.
|
Related Reading VB.NET Language in a Nutshell |
First, the Shape class is defined with the MustInherit keyword. The MustInherit keyword indicates that some other classes must inherit the Shape class; directly instantiating a Shape class is not allowed. For example, the following is not allowed:
Dim someShapes as New Shape
The Area() method definition contains the keyword MustOverride. The MustOverride keyword is used to define an abstract class. That is, the method itself does not contain any implementation; the classes that inherit from it must implement the actual codes.
The Perimeter() method definition contains the keyword Overridable. Although this method contains the actual implementation, any classes inheriting from it have the option to override its implementation.
Listing 3 shows the class Rectangle inheriting from the Shape class (using the Inherits keyword). It implements the Area() method using the Overrides keyword. The keyword MyBase refers to the base class (i.e., Shape).
Listing 3. Rectangle Class
Public Class Rectangle
Inherits Shape
Public Overrides Function Area() As Single
Return MyBase.length * MyBase.breadth
End Function
End Class
You can use the Rectangle class:
Dim r As New Rectangle()
r.length = 4
r.breadth = 5
MsgBox(r.Area)
MsgBox(r.Perimeter)
Listing 4 shows the Square class inheriting from the Rectangle class.
Listing 4. Square Class
Public NotInheritable Class Square
Inherits Rectangle
End Class
The Square class contains the NotInheritable keyword, which indicates that this class cannot be inherited. For example, the following is not allowed:
Public Class smallSquare
Inherits Square
End Class
You can use the Square class:
Dim s As New Square()
s.length = 4
s.breadth = 4
MsgBox(s.Area)
MsgBox(s.Perimeter)
Listing 5 shows the Circle class. It overrides both the Area() and Perimeter() methods.
Listing 5. Circle Class
Public Class Circle
Inherits Shape
Public Overrides Function Area() As Single
Return Math.PI * Math.Pow(MyBase.length / 2, 2)
End Function
Public Overrides Function Perimeter() As Single
Return 2 * Math.PI * MyBase.length / 2
End Function
End Class
You can access the Circle class:
Dim c As New Circle()
c.length = 6
MsgBox(c.Area)
MsgBox(c.Perimeter)
Overloading and Shadowing
Another important concept in object-oriented programming is method overloading. Consider the example in Listing 6.
Listing 6. BaseClass Class
Public Class BaseClass
Public Sub Init(ByVal num As Integer)
MsgBox("Number in BaseClass is " & num)
End Sub
Public Sub Init(ByVal st As String)
MsgBox("String in BaseClass is " & st)
End Sub
End Class
BaseClass contains two methods of the same name, Init. However, these two methods accept different input parameters. One takes an integer, while the other takes a string.
In Listing 7, the class DerivedClass inherits from the BaseClass, and it too contains two methods called Init, both prefixed by the Overloads keyword.
Listing 7. DerivedClass
Public Class DerivedClass
Inherits BaseClass
Overloads Sub Init(ByVal num As Integer)
' overloads the method with the same parameter list
MsgBox("Number in DerivedClass is " & num)
End Sub
Overloads Sub Init(ByVal ch As Char)
' overloads the method
MsgBox("Character in DerivedClass is " & ch)
End Sub
End Class
The following examples should make it clear which methods are active:
'---Shadowing and overloading
Dim d As New DerivedClass()
' prints out "String in BaseClass is Hello VB.NET"
d.Init("Hello VB.NET")
' prints out "Number in DerivedClass is 5"
d.Init(5)
' prints out "Character in DerivedClass is A"
d.Init(Chr(65))
DerivedClass now exposes three methods with the following input parameters:
Public Sub Init(ByVal num As Integer) ' from DerivedClass
Public Sub Init(ByVal ch As Char) ' from DerivedClass
Public Sub Init(ByVal st As String) ' from BaseClass
In Listing 8, I have another class, DerivedClass2, inheriting from BaseClass. This time, I have the method Init() prefixed with the Shadows keyword.
Listing 8. DerivedClass2 Class
Public Class DerivedClass2
Inherits BaseClass
Shadows Sub Init(ByVal num As Integer)
' hides all the different argument list
MsgBox("Number in DerivedClass2 is " & num)
End Sub
End Class
The Shadows keyword will effectively hide all of the other methods in the class, so now there is only one method exposed in the DerivedClass2 class. The following illustrates this:
Dim d2 As New DerivedClass2()
d2.Init(5) ' only one method is exposed
Multiple inheritances of classes are not allowed in VB.NET. That is, you cannot have something like this:
Public Class anotherClass
Inherits BaseClass
Inherits DerivedClass ' Not allowed!
End Class


