Home and Learn: VB Net Course
This tutorial is part of an ongoing lesson. The first part is here: Create your own Classes in VB .NET
In the world of classes and object, Inheritance is when you create a second class based off another. The second class inherits all the functionality of the first class (the parent or base class) but does its own thing as well. For example, suppose you added a parent class that opens text files and writes to text files. You could add a child class that also does these two things, reads and writes text files, but can do things that the parent can't. So, the child class has inherited the ability to read and write to text files. But it could also, say, process the text file and parse the data, something that the parent can't do.
Let's code an example.
Add a new class to your project. Call it InheritanceParent. As the code for the class, you can copy and paste this code:
Public Class InheritanceParent
Private StudentFamilyName As String
Private StudentPersonalName As String
Sub New()
StudentFamilyName = "NA"
StudentPersonalName = "NA"
End Sub
Public Function GetFullName(family As String, personal As String) As String
StudentFamilyName = family
StudentPersonalName = personal
Dim wholeName As String = StudentPersonalName + ", " + StudentFamilyName
Return wholeName
End Function
End Class
The class has two Instance variables, StudentFamilyName and StudentPersonalName. Both are set up as strings.
The class also has a constructor with the two Instance variables set to default values of NA (Not Available). There is also a function that returns a full name when a family and personal name are passed in.
So, that's the parent class. Now add a new class to the project. Call
it InheritanceChild. As the code for this child class, add the
following:
Public Class InheritanceChild
Private CountryOfOrigin As String = "NA"
Public Function AddCountry(country As String) As String
CountryOfOrigin = country
Dim returnString As String = CountryOfOrigin
Return returnString
End Function
End Class
The line to take note of here is the first line of the class:
Inherits InheritanceParent
So you just need the keyword Inherits, followed by the class you want to use as the parent (base class). The child class can then do its own thing. All we're adding is a function that sets the country of origin for a student.
Go back to your form. Add a new button. Double-click the button to create a code stub. As the first line for the button, add this:
Dim country As New InheritanceChild()
We're initialising an object from our child class. The object is called country.
Now add this line:
Dim fullName As String = country.GetFullName("Carney", "Kenny")
The thing to note about this line is that the GetFullName function isn't in the child class. It's in the parent class. We can use it in our child class, however, because the child inherits all the functionality of the parent, which includes the GetFullName function.
Next, add this line:
Dim countryOrigin As String = country.AddCountry("UK")
This time, we're accessing the AddCountry function, which is only available to the child class. (The parent class can't access the AddCountry function.)
Finally, display something on screen with this line:
MessageBox.Show(fullName & ", " & countryOrigin)
Run your code and you should see a full name appear, which is from the parent class. At the end of the message, is the country of origin, which is coming from the child.
One final thing before we leave this topic. You can add a summary to your
functions. This will provide information in a popup when you use the function.
Try it out. Go back to your InheritanceChild class. Just above the function, type three single quotes. Visual Studio will turn the quotes into the following, as soon as you finish typing the third quote:
''' <summary>
'''
''' </summary>
''' <param name="country"></param>
''' <returns></returns>
The summary part is where you can type a summary of what your function does. Visual Studio has worked out that the function only has one parameter, and that it's called country. At the end, you can enter some text to tell the user what your function returns.
Change it all to this:
''' <summary>
''' Get the country of origin of a student.
''' </summary>
''' <param name="country"></param>
''' <returns>The country of origin</returns>
The green parts are the text that will be displayed in your popup.
Save the file and go back to your button code. Hover your mouse over the AddCountry function on the third line of the button code. You should see this:

And there's the text you added for the summary and the value it returns.
We'll leave it there for VB NET classes and objects. Next up, we'll tackle
databases.
Learn about VB .NET and Databases -->
Back to the VB NET Contents Page
Email us: enquiry at homeandlearn.co.uk