Difference Between Interface And Abstract Class in VB.NET

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

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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.