0% found this document useful (0 votes)
106 views8 pages

Explicit, Implicit and Sementics in

In VB.NET, interface implementation semantics define how classes or structures implement interface members, either implicitly or explicitly. Implicit implementation allows direct access to methods via the class, while explicit implementation restricts access to the interface only, helping to avoid naming conflicts and providing cleaner class APIs. Key aspects include multiple interface implementation, access control, and the requirement for casting when accessing explicitly implemented members.

Uploaded by

mohinikatre588
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views8 pages

Explicit, Implicit and Sementics in

In VB.NET, interface implementation semantics define how classes or structures implement interface members, either implicitly or explicitly. Implicit implementation allows direct access to methods via the class, while explicit implementation restricts access to the interface only, helping to avoid naming conflicts and providing cleaner class APIs. Key aspects include multiple interface implementation, access control, and the requirement for casting when accessing explicitly implemented members.

Uploaded by

mohinikatre588
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

In VB.

NET, Interface Implementation Semantics refers to


how a class or structure implements the members (methods,
properties, events) defined in an interface. It encompasses the
rules and behaviors of interface implementation, such as
explicit and implicit implementation, access control, and
name resolution, when a class implements one or more
interfaces.
Key Aspects of Interface Implementation Semantics in
VB.NET:
1. Implicit Implementation:
When a class or structure implements an interface method by
providing a method with the exact same name and signature
as defined in the interface, this is known as implicit
implementation. The method is publicly accessible via both
the class and the interface.
Example:
Output: Implicitly Implemented

2. Explicit Implementation:
With explicit implementation, the class provides
implementation for an interface member, but the method is
only accessible through the interface, not through the class
directly. Explicit implementation is useful when a class
implements multiple interfaces that have methods with the
same name or when you want to control the visibility of the
interface members.
Example:
Outputput: peron wali class
Anotherperson wali class

In this example:
 The Display() method from each interface (Person and
Anotherperson) is implemented explicitly in
SampleClass. To call these methods, you must first cast
the object to the appropriate interface type.
3. Multiple Interface Implementation:
A class can implement multiple interfaces. The semantics of
how this is done include ensuring that the class provides
implementation for all the members of each interface it
implements. If two interfaces have members with the same
signature, explicit implementation is often required to avoid
ambiguity.
4. Inheritance and Interface Implementation:
A class that inherits another class and implements an interface
must either provide its own implementation of the interface
members or use the inherited class’s implementation (if
available).
5. Access Control:
Explicitly implemented members are not accessible directly
from an instance of the class, only through the interface.
Implicitly implemented members follow normal access
modifiers (such as Public, Private).
Summary:
In VB.NET, interface implementation semantics dictate
how a class or structure should implement the members of an
interface, either implicitly or explicitly. This gives flexibility
and control over how interface members are exposed and
accessed, especially when dealing with multiple interfaces
that might have overlapping members.

Explicit Interface in detail

The key points of explicit interface implementation in


VB.NET are:
1. Access Through Interface Only:
Explicitly implemented members are only accessible when the
object is cast to the specific interface. They cannot be
accessed directly via the class instance.
Example:
Dim example As IExample = New ExampleClass()
example.Display() ' Works because example is cast to
IExample

2. Avoids Name Conflicts:


Explicit interface implementation is particularly useful when a
class implements multiple interfaces that have methods with
the same name. By explicitly implementing each method, you
can avoid conflicts between interface members.
Example:
Public Interface IExample
Sub Display()
End Interface

Public Interface IAnotherExample


Sub Display()
End Interface

' Explicit implementation allows both interfaces to be


implemented without conflict.
3. Interface-Specific Behaviour:
Each interface’s member can have its own specific behaviour,
which is isolated and accessed only when the object is cast to
that particular interface. This allows finer control over the
interface behaviour.

4. Hides Implementation from Class Level:


Explicit interface members are hidden from direct access at
the class level, making the class’s public API cleaner and less
cluttered.
5. Method Signature:
When explicitly implementing an interface, the method must
be qualified with the interface name. For example:
Private Sub IExample_Display() Implements
IExample.Display
' Method body
End Sub
This syntax clearly identifies which interface’s method is
being implemented.
6. Casting is Required:
To invoke an explicitly implemented member, you must cast
the object to the appropriate interface type:
Dim obj As New SampleClass()
Dim example As IExample = CType(obj, IExample)
example.Display()
7. Not Accessible via Class Reference:
Unlike implicitly implemented members, explicitly
implemented methods cannot be called directly from the class
object. They are entirely hidden from the class's public API
unless accessed via the interface.
Summary:
 Access is only through the interface.
 Helps avoid naming conflicts in multiple interface
implementations.
 Provides clean separation of interface behavior.
 Explicit members are hidden from direct access in the
class, requiring casting.

Difference between explicit and implicit interface


Explicit Interface Implicit Interface
Feature
Implementation Implementation

Accessible via both the


Access to Accessible only via the
class instance and the
Members interface reference.
interface reference.

Methods are qualified with Methods are not qualified


Method
the interface name (e.g., with the interface name,
Naming
IExample.Display). just the method name.

Direct Access Cannot be called directly Can be called directly


from Class from the class instance. from the class instance.

Helps avoid naming No specific mechanism


Usage with
conflicts when to handle name conflicts;
Multiple
implementing methods care must be taken to
Interfaces
from multiple interfaces. avoid them.

Members are hidden from


Members are visible as
the class’s public API;
Visibility part of the class’s public
only accessible via
API.
casting.

Easier and simpler


Provides more control
implementation for
Flexibility over how interface
interfaces with unique
members are exposed.
method names.
Requires casting to the No casting required; can
Casting
interface to access the call methods directly
Required
method. from the class instance.

Useful when implementing


Useful when methods are
multiple interfaces with
unique and should be part
When to Use the same method names or
of the class’s public
when wanting to control
behavior.
interface method visibility.

You might also like