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
In this article we will learn What is the difference between interface and abstract class in VB.NET.
Interfaces:-
-
A user defined data type similar to class but contains all abstract methods.
-
All methods are abstract and public by default.
-
All such methods are overridden in child class.
-
Allows to implement the multiple inheritance.
-
A class can inherit only one other class but any number of interfaces.
-
All interfaces in .NET starts with I.
-
implements keyword to implement the interface.
-
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.
- 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.
- 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.
- 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.
- 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.
- Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
- Abstract classes are faster than interfaces.