0% found this document useful (0 votes)
53 views27 pages

Interfaces: Prof. M Nataraja Suresh

Interfaces provide a contract that defines the methods, properties, and events that a class must support. When a class implements an interface, it guarantees to support the interface's members. Interfaces allow for polymorphism, as multiple classes can implement the same interface and be treated interchangeably through the interface type. Explicit interface implementation is used when two interfaces share a member name to avoid conflicts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views27 pages

Interfaces: Prof. M Nataraja Suresh

Interfaces provide a contract that defines the methods, properties, and events that a class must support. When a class implements an interface, it guarantees to support the interface's members. Interfaces allow for polymorphism, as multiple classes can implement the same interface and be treated interchangeably through the interface type. Explicit interface implementation is used when two interfaces share a member name to avoid conflicts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Interfaces

Prof. M Nataraja Suresh


What is an Interface?
• An interface is a contract that guarantees to a client how a class or
struct will behave.
• When a class implements an interface, it tells any potential client "I
guarantee I'll support the methods, properties, events, and indexers
of the named interface.“
• Syntactically, an interface is like a class that has only abstract
methods.
• An abstract class serves as the base class for a family of derived
classes, while interfaces are meant to be mixed in with other
inheritance trees.
• contracts are made manifest using the interface keyword, which
declares a reference type that encapsulates the contract
What is an Interface?
• When a class implements an interface, it must implement all the methods
of that interface; in effect the class says "I agree to fulfil the contract
defined by this interface.“
• Inheriting from an abstract class implements the is-a relationship
• Implementing an interface defines implements relationship
Ex: A car is a vehicle, but it might implement the CanBeBoughtWithABigLoan
capability (as can a house, for example).
• Method declarations of an Interface do not include access modifiers
• method declarations do not include access modifiers
• Interface methods are implicitly public because an interface is a contract
meant to be used by other classes.
• You cannot create an instance of an interface; instead you instantiate a class
that implements the interface.
Defining and Implementing an Interface?
[attributes ] [access-modifier ] interface interface-name [: base-list ]
{interface-body }
• It is common (but not required) to begin the name of your interface
with a capital ‘I’ (thus, IStorable, ICloneable, IClaudius, etc.)
• The base-list lists the interfaces that this interface extends
• Let us create an interface, IStorable that describes the methods and
properties a class needs to be stored to and retrieved from a database
or other storage such as a file
Implementing More Than One Interface
• Suppose if your Document class can be stored and it also can be
compressed, you might choose to implement both the IStorable and
ICompressible interfaces
public class Document : IStorable, ICompressible
Extending Interfaces
• It is possible to extend an existing interface to add new methods or
members, or to modify how existing members work
• might extend ICompressible with a new interface,
ILoggedCompressible, which extends the original interface with
methods to keep track of the bytes saved
Extending and Combining Interfaces
• If a class does implement ILoggedCompressible, it must implement all
the methods of both ILoggedCompressible and ICompressible.
• Objects of ILoggedCompressible type can be cast either to
ILoggedCompressible or to Icompressible
Combining Interfaces - We can create new interfaces by combining
existing interfaces, and, optionally, adding new methods or properties.
For example, you might decide to create IStorableCompressible.
• This interface would combine the methods of each of the other two
interfaces, but would also add a new method to store the original size
of the precompressed item
Accessing Interface Methods
• You can access the members of the IStorable interface as if they were
members of the Document class

• We can also create an instance of the interface by casting the


document to the interface type, and then use that interface to access
the methods:
Accessing Interface Methods
• Access through an interface allows you to treat the interface
polymorphically.
• In other words, you can have two or more classes implement the
interface, and then by accessing these classes only through the
interface, you can ignore their real runtime type and treat them
interchangeably
The is operator would like to be able to ask the object if it supports the
interface (in order to invoke the appropriate methods)
The as operator
• The as operator combines the is and cast operations by testing first to
see whether a cast is valid (i.e., whether an is test would return true)
and then completing the cast when it is
Interface Versus Abstract Class
• C# does not allow multiple inheritance with classes.
• However, C# does allow you to implement any number of interfaces
and derive from one base class.
• Thus, by making Storable an interface, you can inherit from the List
class and also from IStorable
Overriding Interface Implementations
• An implementing class is free to mark any or all of the methods that
implement the interface as virtual
• For example, a Document class might implement the IStorable
interface and mark the Read() and Write() methods as virtual.
• The Document might Read() and Write() its contents to a File type.
The developer might later derive new types from Document, such as a
Note or EmailMessage type, and he might decide that Note will read
and write to a database rather than to a file.
Explicit Interface Implementation
• if the class implements two interfaces, each of which has a method
with the same signature?
• Say, IStorable and ITalk. The latter implements a Read( ) method that
reads a book aloud. Unfortunately, this conflicts with the Read( )
method in Istorable
• With explicit implementation, the implementing class explicitly
identifies the interface for the method like:
void ITalk.Read( )
• there is no need to use explicit implementation with the other
method of Talk, Because there is no conflict:
public void Talk( )
Explicit Interface Implementation
• you cannot access the explicitly implemented method through the
object itself. When you write:
theDoc.Read( );
the compiler assumes you mean the implicitly implemented interface
for IStorable.
The only way to access an explicitly implemented interface is through a
cast to an interface: (Selectively Exposing Interface Methods)
Member Hiding - Possibility
• It is possible for an interface member to become hidden.
• For example, suppose you have an interface IBase that has a property
P:

• you derive from that interface a new interface, IDerived, which hides
the property P with a new method P( ): Making property P hidden
Member Hiding - Possibility
• We can use explicit implementation for either the base property or
the derived method, (or) Explicit on both
Accessing Sealed Classes and Value Types
• it is preferable to access the methods of an interface through an
interface cast. The exception is with value types (e.g., structs) or with
sealed classes.
• In that case, it is preferable to invoke the interface method through
the object.
• When you implement an interface in a struct, you are implementing it
in a value type. When you cast to an interface reference, there is an
implicit boxing of the object.
• Unfortunately, when you use that interface to modify the object, it is
the boxed object, not the original value object, that is modified.

You might also like