Windows Phone Developers

Saturday, September 18, 2010

Abstract Classes in VB.NET

Abstract classes are base classes that has to be inherited.  A class can be made as an abstract with MustInherit modifier

Abstract classes can contain abstract methods also. Abstract methods are the ones where only signature is available in the base class and their implementation is taken care in derived classes.

Following is an example of abstract class:

Public MustInherit Class ClassAbs



    Public Sub ExampleMethod()

        MessageBox.Show("This is sample")

    End Sub

    Public MustOverride Sub ExampleMethod_ShouldBeInherited()



    Public Overridable Sub ExampleMethod_CanBeCalledFromBase()

        MessageBox.Show("This is sample")

    End Sub

End Class


the class contains one Abstract method

The class can be inherited as shown below:

Public Class ClassDerivedFromAbstract
    Inherits ClassAbs

    Public Sub ExampleAbstract()

        MyBase.ExampleMethod_CanBeCalledFromBase()


    End Sub

    Public Overrides Sub ExampleMethod_ShouldBeInherited()


    End Sub

    Public Overrides Sub ExampleMethod_CanBeCalledFromBase()

        MessageBox.Show("This is sample from Derived")

    End Sub

End Class


The class can be used as shown below:

Dim CAb As New ClassDerivedFromAbstract

        CAb.ExampleMethod_CanBeCalledFromBase()
        CAb.ExampleMethod_ShouldBeInherited()


the first call uses the method from the base class and the second one uses the Method we had overriden in the derived class Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment