📘 VB.
NET Interfaces
🔹 What is an Interface?
● An interface in VB.NET is like a contract that defines a set of methods, properties, and
events that a class must implement.
● Unlike a class:
○ It does not provide implementation (only definitions).
○ It supports multiple inheritance (a class can implement multiple interfaces).
● Helps in achieving abstraction and polymorphism.
🔹 Syntax of Interface in VB.NET
Interface IShape
Sub Draw()
Function Area() As Double
End Interface
🔹 Implementing an Interface
Interface IShape
Sub Draw()
Function Area() As Double
End Interface
Class Circle
Implements IShape
Private radius As Double
Public Sub New(r As Double)
radius = r
End Sub
Public Sub Draw() Implements IShape.Draw
Console.WriteLine("Drawing a Circle")
End Sub
Public Function Area() As Double Implements IShape.Area
Return Math.PI * radius * radius
End Function
End Class
Module Program
Sub Main()
Dim c As IShape = New Circle(5)
c.Draw()
Console.WriteLine("Area = " & c.Area())
End Sub
End Module
✅ Output:
Drawing a Circle
Area = 78.5398163397448
🔹 Multiple Interface Implementation
Interface IPrintable
Sub Print()
End Interface
Interface IScannable
Sub Scan()
End Interface
Class MultiFunctionPrinter
Implements IPrintable, IScannable
Public Sub Print() Implements IPrintable.Print
Console.WriteLine("Printing Document...")
End Sub
Public Sub Scan() Implements IScannable.Scan
Console.WriteLine("Scanning Document...")
End Sub
End Class
Module Program
Sub Main()
Dim m As New MultiFunctionPrinter()
m.Print()
m.Scan()
End Sub
End Module
✅ Output:
Printing Document...
Scanning Document...
🔹 Key Points about Interfaces
● Cannot contain fields (only method, property, event definitions).
● All members are public by default.
● A class that implements an interface must implement all members.
● Interfaces provide loose coupling in applications.