Implementing inheritance in VB.Net
VB.Net uses the
Inherits keyword to indicate inheritance. Suppose we have a class named
Student with the following fields;
mRegistrationNumber, mName and
mDateOfBirth along with the corresponding properties. The class also has a function called
GetAge() which calculates and returns the age of a Student.
Class Student
' private Fields
Private mRegistrationNumber As Integer
Private mName As String
Private mDateOfBirth As DateTime
' Student Constructor
Public Sub New()
Console.WriteLine("New student created. " &
"Parameter less constructor called...")
End Sub
Public Sub New(ByVal pRegistrationNumber As Integer, _
ByVal pName As String, _
ByVal pDateOfBirth As DateTime)
mRegistrationNumber = pRegistrationNumber
mName = pName
mDateOfBirth = pDateOfBirth
Console.WriteLine("New Student Created. " &
"Parameterized constructor called...")
End Sub
' public Properties
Public ReadOnly Property RegisterationNumber() As Integer
Get
Return mRegistrationNumber
End Get
End Property
Public Property Name() As String
Get
Return Name
End Get
Set(ByVal Value As String)
Name = Value
End Set
End Property
Public Property DateOfBirth() As DateTime
Get
Return DateOfBirth
End Get
Set(ByVal Value As DateTime)
DateOfBirth = Value
End Set
End Property
' public Function
Public Function GetAge() As Integer
Dim age As Integer = DateTime.Now.Year - DateOfBirth.Year
Return age
End Function
End Class
The
Student class above is very simple. We have defined three
Private fields, their accessor properties and one function to calculate the age of a student. We have defined two constructors: the first one takes no parameters and the other takes the values of three parameters. Note that we have only defined the
Get property of
mRegistrationNumber since we don't want the user of the
Student class to change the
mRegistrationNumber once it is assigned through constructor. Also, note that we did not make
GetAge() a property but a function. The reason for this is that properties are generally supposed to be accessors for getting/setting values of fields and not for calculating/processing data. Hence, it makes sense to declare
GetAge() as a function.
Let us declare another class named
SchoolStudent that inherits the
Student class but with additional members such as marks of different subjects and methods for calculating total marks and percentages.
Class SchoolStudent
Inherits Student
' Private Fields
Private mTotalMarks As Integer
Private mTotalObtainedMarks As Integer
Private mPercentage As Double
' Public Constructors
Public Sub New()
Console.WriteLine("New school student created. " &
"Parameter less constructor called...")
End Sub
Public Sub New(ByVal pRegNum As Integer, _
ByVal pName As String, _
ByVal pDob As DateTime, _
ByVal pTotalMarks As Integer, _
ByVal pTotalObtainedMarks As Integer)
' call to base class constructor
MyBase.New(pRegNum, pName, pDob)
mTotalMarks = pTotalMarks
mTotalObtainedMarks = pTotalObtainedMarks
Console.WriteLine("New school student is created. " &
"Parameterized constructor called...")
End Sub
' Public Properties
Public Property TotalMarks() As Integer
Get
Return mTotalMarks
End Get
Set(ByVal Value As Integer)
mTotalMarks = Value
End Set
End Property
Public Property TotalObtainedMarks() As Integer
Get
Return mTotalObtainedMarks
End Get
Set(ByVal Value As Integer)
mTotalObtainedMarks = Value
End Set
End Property
' Public Function
Public Function GetPercentage() As Double
mPercentage = TotalObtainedMarks / TotalMarks * 100
Return mPercentage
End Function
End Class
Our
SchoolStudent class inherits
Student class by using the
Inherits keyword
Class SchoolStudent
Inherits Student
The
SchoolStudent class inherits all the members of the
Student class. In addition, it also declares its own members: three private fields (
mTotalMarks, mTotalObtainedMarks and
mPercentage) with their corresponding properties, two constructors (a parameter less one and a parameterized one) and one instance the function (
GetPercentage()). For now, forget about the second (parameterized) constructor of our
SchoolStudent class. Lets make our
Test Module and
Main() method.
' program to demonstrate inheritance
Module Test
Public Sub Main()
Dim st As New Student(1, "Fraz", New DateTime(1980, 12, 19))
Console.WriteLine("Age of student, {0}, is {1}" & vbCrLf, _
st.Name, st.GetAge())
Dim schStd = New SchoolStudent()
schStd.Name = "Newton"
schStd.DateOfBirth = New DateTime(1981, 4, 1)
schStd.TotalMarks = 500
schStd.TotalObtainedMarks = 476
Console.WriteLine("Age of student, {0}, is {1}. {0} got {2}% marks.", _
schStd.Name, schStd.GetAge(), schStd.GetPercentage())
End Sub
End Module
In the
Main() method, first we made an object of the
Student class (
st) and printed the
name and
age of a Student
st. Next, we made an object of the
SchoolStudent class (
schStd). Since, we used parameter less constructor to instantiate
schStd, we set the values of its fields through properties and then printed the name, age and percentage of
SchoolStudent (schStd). Note that we are able to access the properties
Name and
DateOfBirth (defined in
Student class) because our
SchoolStudent class is inherited from the
Student class, thus inheriting the public properties too. When we execute the above program, we get the following output
New Student Created. Parameterized constructor called...
Age of student, Fraz, is 24
New student created. Parameter less constructor called...
New school student created. Parameter less constructor called...
Age of student, Newton, is 23. Newton got 95.2% marks.
Press any key to continue
The output of the first two lines is as expected. But, notice the output when we create the
SchoolStudent object. First, the parameter less constructor of
Student is called and then the constructor of
SchoolStudent is called.
New student created. Parameter less constructor called...
New school student created. Parameter less constructor called...