Interface
Interface
Common Uses of Interface
- Interface is used to collect similarities. Wherever we have to implement in the class means follow that similarities
Wherever we have to use
- Two person using same code but connection to the database methods only differ to each other. One person use sql based connection another one use oracle based connection. This type of scenario we can use Interface.
For Example
-
A human and a parrot can both whistle, however it would not make sense to represent
Humans andParrots as subclasses of aWhistlerclass, rather they would most likely be subclasses of anAnimalclass (likely with intermediate classes), but would both implement theWhistlerinterface
What is interface
-
The behavior of classes
-
All methods of an interface are abstract methods
-
They cannot have bodies
-
You cannot create an instance from an interface
-
An interface can only be implemented
-
This is the concept of encapsulation
- During runtime, actual object instance is associated
with the interface type
-
It needs only the interface at the compile time
-
Interface does not contain constructor.
- It not supports multiple inheritance directly but using interfaces we can achieve multiple inheritance.
Defining Classes and Interfaces in VB .NET
Public Class AClass
End Class
Public Interface AnInterface
End Interface
Implementing Interfaces
Public Interface AnInterface
Function WhoAmI() As String
End Interface
Public Class AClass
Implements AnInterface
Public Function WhoAmI() As String Implements AnInterface.WhoAmI
Return “AClass”
End Function
End Class