ARTICLE

What is the difference between interface and abstract class in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic 2010 January 24, 2011
In this article we will learn What is the difference between interface and abstract class in VB.NET.
 
Reader Level:

In this article we will learn What is the difference between interface and abstract class in VB.NET.

Interfaces:-

  1. A user defined data type similar to class but contains all abstract methods.

  2. All methods are abstract and public by default.

  3. All such methods are overridden in child class.

  4. Allows to implement the multiple inheritance.

  5. A class can inherit only one other class but any number of interfaces.

  6. All interfaces in .NET starts with I.

  7. implements keyword to implement the interface.

  8. use the Interface keyword to create an interface.

 

The following code demonstrates the use of interface.

 

Module Module1

    Interface Common

        Sub Leaves()

    End Interface

    Interface IHr

        Inherits Common

        Sub ShowSalary()

    End Interface

    Interface IFinance

        Inherits Common

        Sub Budget()

    End Interface

    Class ERP

        Implements IHr

        Implements IFinance

        Public Sub ShowSalary() Implements IHr.ShowSalary

            System.Console.WriteLine("Salary will be on 10th")

        End Sub

        Public Sub Budget() Implements IFinance.Budget

            System.Console.WriteLine("Budget is 10 L")

        End Sub

        Public Sub Leaves() Implements Common.Leaves

            System.Console.WriteLine("Leaves are 10 Cs, 20 EL")

        End Sub

    End Class

 

    Class ITC

        Public Shared Sub Main()

            Dim h As IHr = New ERP()

            h.ShowSalary()

            h.Leaves()

        End Sub

    End Class

End Module

 

The mustinherit keyword is used to create abstract classes in VB.NET.

Abstract Class

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

Example

Module Module1

    Public MustInherit Class AbstractClass

        Public MustOverride Function Add() As Integer

        Public MustOverride Function Mul() As Integer

    End Class

 

    Public Class AbstractOne

        Inherits AbstractClass

        Dim i As Integer = 20

        Dim j As Integer = 30

 

        Public Overrides Function Add() As Integer

            Return i + j

        End Function

        Public Overrides Function Mul() As Integer

            Return i * j

        End Function

    End Class

 

    Sub Main()

        Dim abs As New AbstractOne()

        WriteLine("Sum is" & " " & abs.Add())

        WriteLine("Multiplication is" & " " & abs.Mul())

        Read()

    End Sub

 

End Module

 

Difference between interface and abstract class in VB.NET.

These are some differences between abstract class and interfaces.

  1.  An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
  2. An abstract class can contain fields,constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
  3. An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
  4. A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
  5. Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
  6. Abstract classes are faster than interfaces.

Login to add your contents and source code to this article
share this article :
post comment
 

friend, shared in visual basic is used as static in C#. A shared method is not accessed via an object instance like a regular method, but rather is accessed directly from the class. The following is a simple example of a shared method: Public Class Math Shared Function Add(ByVal a As Integer, ByVal b As Integer) As Integer Return a + b End Function End Class We can use this method – without instantiating a Math object – as follows: Dim result As Integer result = Math.Add(5, 10)

Posted by Rohatash Kumar Jan 27, 2011

what is 'shared' in shared sub main()..thanks

Posted by nikhil kulkarni Jan 25, 2011

I added 'shared sub main' as (instructed double clicked the error) and was asked to choose a module..it is done thanks

Posted by nikhil kulkarni Jan 25, 2011

To remove this error double click on the error then a box will display after that double click on the error on the box. this error will remove automatically.

Posted by Rohatash Kumar Jan 24, 2011

i got an error as sub main() not found in module1...is it because of class ITC

Posted by nikhil kulkarni Jan 24, 2011
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor