Visual Basic Inheritance

In visual basic, Inheritance is one of the primary concepts of object-oriented programming (OOP) and it is useful to inherit the properties from one class (base) to another (child) class.

 

The inheritance will enable us to create a new class by inheriting the properties from other classes to reuse, extend and modify the behavior of other class members based on our requirements.

 

In visual basic inheritance, the class whose members are inherited is called a base (parent) class and the class that inherits the members of base (parent) class is called a derived (child) class.

Visual Basic Inheritance Syntax

Following is the syntax of implementing an inheritance to define derived class that inherits the properties of base class in a visual basic programming language.

 

<access_modifier> Class <base_class_name>

    // Base class Implementation

End Class 

 

<access_modifier> Class <derived_class_name>

Inherits base_class_name

    // Derived class implementation

End Class

If you observe the above syntax, we are inheriting the properties of base class into child class to improve the code reusability.

 

Following is the simple example of implementing inheritance in a visual basic programming language.

 

Public Class X

    Public Sub GetDetails()

        ' Method implementation

    End Sub

End Class

 

Public Class Y

    Inherits X

    ' your class implementation

End Class

 

Class Program

    Public Shared Sub Main(ByVal args As String())

        Dim y As Y = New Y()

        y.GetDetails()

    End Sub

End Class

If you observe the above example, we defined a class “X” with a method called “GetDetails” and the class “Y” is inheriting from the class “X”. After that, we are calling a “GetDetails” method by using an instance of derived class “Y”.

 

In visual basic, it’s not possible to inherit the base class constructors in the derived class and the accessibility of other members of the base class also depends on the access modifiers which we used to define those members in the base class.

Visual Basic Inheritance Example

Following is the example of implementing an inheritance by defining two classes in a visual basic programming language.

 

Public Class User

    Public Name As String

    Private Location As String

    Public Sub New()

        Console.WriteLine("Base Class Constructor")

    End Sub

    Public Sub GetUserInfo(ByVal loc As String)

        Location = loc

        Console.WriteLine("Name: {0}", Name)

        Console.WriteLine("Location: {0}", Location)

    End Sub

End Class

 

Public Class Details

    Inherits User

    Public Age As Integer

    Public Sub New()

        Console.WriteLine("Child Class Constructor")

    End Sub

    Public Sub GetAge()

        Console.WriteLine("Age: {0}", Age)

    End Sub

End Class

 

Class Program

    Public Shared Sub Main(ByVal args As String())

        Dim d As Details = New Details()

        d.Name = "Suresh Dasari"

        ' Compile Time Error

        ' d.Location = "Hyderabad";

        d.Age = 32

        d.GetUserInfo("Hyderabad")

        d.GetAge()

        Console.WriteLine(vbLf & "Press Any Key to Exit..")

        Console.ReadLine()

    End Sub

End Class

If you observe the above example, we defined a base class called “User” and inheriting all the properties of User class into a derived class called “Details” and we are accessing all the members of User class with an instance of Details class.

 

If we uncomment the commented code, we will get a compile-time error because the Location property in User class is defined with a private access modifier and the Private members can be accessed only within the class.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic (VB) Inheritance Example Result

 

If you observe the above result, we are able to access all the properties of base class into child class based on our requirements.

Visual Basic Multi-Level Inheritance

Generally, visual basic supports only single inheritance that means a class can only inherit from one base class. However, in visual basic the inheritance is transitive and it allows you to define a hierarchical inheritance for a set of types and it is called a multi-level inheritance.

 

For example, suppose if class C is derived from class B, and class B is derived from class A, then class C will inherit the members declared in both class B and class A.

 

Public Class A

    ' Implementation

End Class

 

Public Class B

    Inherits A

    ' Implementation

End Class

 

Public Class C

    Inherits B

    ' Implementation

End Class

If you observe the above code snippet, class C is derived from class B, and class B is derived from class A, then class C inherits the members declared in both class B and class A. This is how we can implement multi-level inheritance in our applications.

Visual Basic Multi-Level Inheritance Example

Following is the example of implementing multi-level inheritance in a visual basic programming language.

 

Public Class A

    Public Name As String

    Public Sub GetName()

        Console.WriteLine("Name: {0}", Name)

    End Sub

End Class

 

Public Class B

    Inherits A

    Public Location As String

    Public Sub GetLocation()

        Console.WriteLine("Location: {0}", Location)

    End Sub

End Class

 

Public Class C

    Inherits B

    Public Age As Integer

    Public Sub GetAge()

        Console.WriteLine("Age: {0}", Age)

    End Sub

End Class

 

ClassProgram

    Public Shared Sub Main(ByVal args As String())

        Dim c As C = New C()

        c.Name = "Suresh Dasari"

        c.Location = "Hyderabad"

        c.Age = 32

        c.GetName()

        c.GetLocation()

        c.GetAge()

        Console.WriteLine(vbLf & "Press Any Key to Exit..")

        Console.ReadLine()

    End Sub

End Class

If you observe the above example, we implemented three classes (A, B, C) and class C is derived from class B, and class B is derived from class A.

 

By implementing multi-level inheritance, class C can inherit the members declared in both class B and class A.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic Multilevel Inheritance Example Result

Visual Basic Multiple Inheritance

As discussed, visual basic supports only single inheritance that means a class can only inherit from one base class. In case, if we try to inherit a class from multiple base classes, then we will get a compile-time error.

 

For example, if class C is trying to inherit from Class A and B at the same time, we will get a compile-time error because multiple inheritance is not allowed in visual basic.

 

Public Class A

    ' Implementation

End Class

 

Public Class B

    ' Implementation

End Class

 

Public Class C

    Inherits A

    Implements B

    ' Implementation

End Class

If you observe the above code snippet, class C is trying to inherit properties from both classes A and B at the same time which will lead to compile-time error like “Implemented type must be an interface”.

 

As discussed, multi-level inheritance is supported in visual basic but multiple inheritance is not supported. In case, if you want to implement multiple inheritance in visual basic, that we can achieve by using interfaces. In the next chapters, we will learn how to use interfaces to achieve multiple inheritance in a detailed manner.