Inheritance is a mechanism in Visual Basic .NET through which one class can derive the functionality (fields and methods) from another class. The class that is being inherited is known as the base class (or superclass), and the class that inherits the base class is known as the derived class (or subclass).
To create a derived class in Visual Basic .NET, you need to use the Inherits keyword and specify the name of the base class after it. Here's an example:
``vb.net
Public Class Animal
Public Property Name As String
Public Sub New(name As String)
Me.Name = name
End Sub
Public Overridable Sub Speak()
Console.WriteLine("I am an animal.")
End Sub
End Class
Public Class Dog
Inherits Animal
Public Sub New(name As String)
MyBase.New(name)
End Sub
Public Overrides Sub Speak()
Console.WriteLine("I am a dog named {0}.", Name)
End Sub
End Class
vb.netAnimal
Theclass is the base class that has a propertyNameand a methodSpeak(). TheDogclass is the derived class that inherits theNameproperty andSpeak()method from theAnimalclass. It also has its own constructor that calls theAnimalclass constructor using theMyBasekeyword.OverrideThe
keyword is used in theSpeak()method of theDogclass to indicate that it is overriding theSpeak()method of theAnimalclass. This means that when theSpeak()method is called on aDogobject, it will execute the code in theDogclass'sSpeak()method instead of theAnimalclass'sSpeak()method.DogHere's an example of how to use the
class:
Dim myDog As New Dog("Fido")
myDog.Speak() ' Output: I am a dog named Fido.
`
In this example, we create a new
Dog object with the name "Fido" and call its Speak() method. The output will be "I am a dog named Fido.", which is the custom message defined in the Dog class's Speak()` method.
In summary, inheritance in Visual Basic .NET allows you to create new classes based on existing ones, which can save you time and effort in writing new code. By reusing the code from the base class, you can focus on adding new functionality to the derived class.