INTERFACE
Interface is same as a class but the only difference is class
can contain both declarations and definition of methods, but
interface can contain only declaration of members such as a
methods, properties, indexers and events.
By default, the members of interface are public and we
are not allowed to include any other access modifier.
In visual basic, an interface cannot be instantiated
directly, but it can be
Instantiated by a class that implements an interface.
Implements keywords is used to implement interface by
class.
The class that implements an interface must provide an
implementation for all the members that are specified in
the interface definition.
The class can implement multiple interfaces.
Syntax:
Interface interface-name
Method declarations
End Interface
e.g.
Interface interface_test
Sub display ()
End Interface
INTERFACE IMPLEMANTATION
Using “Implements” Keyword
Class class_name
Implements interface_name1, interface_name2…
………………………………………..
End class
e.g.:
Class classA
Implements interface_test
Public Sub display() Implements interface_test.display()
Console.WriteLine(“Welcome”)
End Sub
End Class
Notes: “public” modifier is must in method definition within a
class.
Output: Welcome
Multiple Interface Implementation
Multiple implementation
More than one interface can be implemented in a single
class.
e.g.:
Interface inter1
Sub show()
End Interface
Interface inter2
Sub display()
End Interface
Class classA
Implements inter1, inter2
……………………………
End class
Interface
Support multiple inheritance.
Properties, methods and events can be members of
interface.
Variable cannot be declared into interface.
Interface consists of only declaration of members but no
implementation.
Class implementation these members.
Implements keywords is used for their implementation.
One class can inherit only one base class but implements
any number of interfaces.
Interface Class
Instance variable Instance variable can
cannot be declared be declared
Interface cannot Class provides
provide any implementation of
implementation methods
Not have constructor Have constructor
One class can One class can inherit
implement multiple only one class
interfaces
Implements keyword Inherits keyword is
is used to implement used to inherits class
interfaces