Inheritance Basics - Visual Basic.Net | TrickCode

Inheritance Basics - Visual Basic.Net | TrickCode,VB.NET ,inheritance,VB.NET Inheritance
Share it:
Inheritance Basics In Vb.net


Introduction

Just as humans may inherit many different qualities from their parents, classes in VB.NET can also inherit functionality from other classes. A class that is inherited from is called base class whereas the inheriting class is called the derived class. A class inheriting from a base class has access to all of the non-private members of the base class. Inheritance is a powerful feature of .NET that gives programming new and exciting dimensions. With inheritance, we are able to extend the default behaviors of classes by adding extra functionality. Through it, we can re-use code. Because of inheritance, every single class in the .NET universe has access to a method called ToString. Are you wondering how? Well, recall that any class in VB.NET ultimately inherits from the master base class System.Object that defines the ToString virtual method and is inherited by all other classes.

VB.NET Inheritance

Next, we will look at an example that shows how inheritance can be implemented in VB.NET. In Code: Shape base class, we have an abstract base class called Shape that represents any shape (rectangle, triangle, circle, etc) and contains common functionality about a shape. An abstract class (it's marked with the MustInherit keyword) can not be made an instance of, it only works as a starting point for derived classes that are real and specific (e.g. a triangle in our discussion).




Public MustInherit Class Shape
    Private _name As String

    'Derived classes must provide this parameter in their constructors
    Public Sub New(ByVal name As String)
        _name = name
    End Sub

    'Derived classes have direct access to this property
    Protected ReadOnly Property Name() As String
        Get
            Return _name
        End Get
    End Property

    'Derived classes must override this method    
    Public MustOverride Sub Draw()

End Class

The Shape class defines a single field called _name. This is the name of the shape that it represents (other common information about shape is not shown to simplify the example). The class also defines an abstract method called Draw that must be overridden by derived classes. This method can then be called on any shape and the shape will be drawn as every deriving shape must provide an implementation for the method.


VB.NET Inheritance




Next, we have the Triangle class that inherits from the abstract base class Shape and overrides its abstract member(s). This is shown below. The Triangle class starts by inheriting from the abstract base class Shape and overrides its single abstract method Draw (note the Overrides keyword in its signature). Also, note that since the Triangle class inherits from an abstract class and the abstract class has a constructor that takes a single parameter (i.e. the name of the shape), it (the Triangle class) must provide that single parameter to its base class as the first call in its constructor. This is because the Shape class is abstract and can not be created an instance of (only through derived classes) so derived classes are responsible for providing any parameters that the base class defines.





Public Class Triangle
    Inherits Shape

    Public Sub New(ByVal name As String)
        'Passing the incoming parameter to the base class's constructor
        MyBase.New(name)
    End Sub

    Public Overrides Sub Draw()
        'Code to draw a triangle 
    End Sub

End Class



                                        Share this article with your friends
Share it:

VB.NET

Post A Comment:

0 comments: