Inheritance is a technique to derive a type from another
type and the derived type inherits the functionality of the type it is derived
from. The derived type is called a derived, child, or inherited type and the
type it is derived from is called a base or parent type.
In Visual Basic .NET, the Inherits keyword is used to derive
a class. Syntax of Inherits keyword
looks like this code snippet, where ClassB is the base class and ClassA is the
derived class.
Public Class ClassB
Inherits
ClassA
End Class
Here is another example where Author class is derived from
Person class.
Public Class Author
Inherits
Person
End Class
Once a class is inherited from a base class, it inherits all
properties, attributes, and functions from the base class.
Let's see a complete example.
I create a Person class with two properties Name and Age as
you can see in Listing 1.
''' <summary>
''' Person class with Name and Age properties
''' </summary>
''' <remarks></remarks>
Public Class Person
Protected pName As String
Protected pAge As Integer
''' <summary>
''' Name of the person
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property
Name() As String
Get
Return Me.pName
End Get
Set(ByVal value As String)
Me.pName = value
End Set
End Property
''' <summary>
''' Age of the person
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Age()
As Integer
Get
Return Me.pAge
End Get
Set(ByVal value As Integer)
Me.pAge = value
End Set
End Property
End Class
Listing 1
Now, I inherit a class called Author from Person class.
Class Author has three properties BookTitle, PublishedYear, and BookPublisher
as you can see in Listing 2.
''' <summary>
''' Author class: Inherited from Person
''' </summary>
''' <remarks></remarks>
Public Class Author
Inherits Person
Protected title As String
Protected pubYear As Integer
Protected publisher As String
Public Property
BookTitle() As String
Get
Return Me.title
End Get
Set(ByVal value As String)
Me.title =
value
End Set
End Property
Public Property
PublishedYear() As Integer
Get
Return Me.pubYear
End Get
Set(ByVal value As Integer)
Me.pubYear = value
End Set
End Property
Public Property
BookPublisher() As String
Get
Return Me.publisher
End Get
Set(ByVal value As String)
Me.publisher = value
End Set
End Property
End Class
Listing 2
So now I have Author class
inherited from Person class. That means, the Author class must have all
properties defined in the Person class.
So in my code that uses
the Author class, I write code, I see the Name and Age properties defined in
the Person class are available when I create an instance of the Author class
and use its properties.
As you can see from code
sample in Listing 3, I set and display Name and Age properties of Author class
instance.
Module Module1
Sub Main()
' Create a new author
Dim auth As New Author
' Set author properties
auth.Name = "Mahesh Chand"
auth.Age = 30
auth.BookTitle
= "Graphics Programming with GDI+"
auth.BookPublisher = "Addison
Wesley"
auth.PublishedYear = 2003
' Display author
details
Console.WriteLine("Author Name: "
+ auth.Name)
Console.WriteLine("Author Age: "
+ auth.Age.ToString())
Console.WriteLine("Book Title: "
+ auth.BookTitle)
Console.WriteLine("Book Publisher:
" + auth.BookPublisher)
Console.WriteLine("Published Year:
" + auth.PublishedYear.ToString())
Console.ReadLine()
End Sub
End Module
Listing 3
Important Rules of Inheritance
- .NET Framework, the
Object type is mother of all. All types are inherited from the Object type
by default. In our code above, the Person class is derived from the Object
type.
- Inheritance is
transitive. If type C is derived from type B, and type B
is derived from type A, type C inherits the type members
declared in type B as well as the type members declared in type A.
- A derived type extends,
but cannot narrow, its base type. A derived type can add new type members,
and it can shadow inherited type members, but it cannot remove the
definition of an inherited type member.
- Because an instance of a
type contains all of the type members of its base type, a conversion
always exists from a derived type to its base type.
- All types must have a
base type, except for the type Object. Thus, Object is the
ultimate base type of all types, and all types can be converted to it.
- Circularity in
derivation is not permitted. That is, when a type B
derives from a type A, it is an error for type A
to derive directly or indirectly from type B.