You are on page 1of 15

D E F I N I T I O N A N D D E C L A R AT I O N

I N T E R FA C E I N
C#
D E F I N I T I O N O F A N I N T E R FA C E

An interface in C# defines a contract or a set of


requirements that a class must implement. It
specifies the methods, properties, events, or
indexers that the implementing class should
Click icon to add picture
provide.
BENEFITS OF USING
I N T E R FA C E S
Using interfaces in C# provides several benefits, including:
- Encourages code reusability and modularity
- Supports multiple inheritance through interface implementation
- Enables loose coupling and dependency injection
- Defines a contract for class interactions and expectations
KEY DIFFERENCES

ABSTRACT
CLASSES VS
I N T E R FA C E S
An abstract class in C# is a class that cannot be
ABSTRACT instantiated and is meant to be inherited by
CLASS other classes. It can contain abstract members
that must be implemented by derived classes.
I N T E R FA C E

An interface in C# defines a contract or a set of


requirements that a class must implement. It
specifies the methods, properties, events, or
indexers that the implementing class should Click icon to add picture
provide.
I N S TA N T I AT I O N

Abstract classes cannot be instantiated Interfaces cannot be instantiated either.


directly. They can only be used as base They are implemented by classes.
classes for other classes.
I N H E R I TA N C E

A class can inherit only one abstract A class can implement multiple
class. Multiple inheritance is not interfaces, providing flexibility and
allowed. allowing for the implementation of
multiple contracts.
M E T H O D I M P L E M E N TAT I O N

Abstract classes can contain both Interfaces only contain method


abstract and non-abstract methods. declarations. Implementing classes
Derived classes must provide must provide implementations for all
implementations for all the abstract the interface methods.
methods.
ACCESS MODIFIERS

Interfaces do not specify access

Abstract classes can have access modifiers. All interface members are

modifiers for methods and members. public by default.


USAGE GUIDELINES

Use abstract classes when you want to Use interfaces when you want to define
provide a common base a contract for multiple unrelated
implementation and enforce derived classes to adhere to.
classes to implement specific behavior.
DEFINITION AND USAGE

DERIVED
I N T E R FA C E S I N
C#
DEFINITION OF
DERIVED
I N T E R FA C E
In C#, a derived interface is an interface that extends or
inherits from another interface. It can inherit the members
(methods, properties, events, or indexers) of the base
interface and can add its own members.
USAGE OF DERIVED INTERFACE

Derived interfaces are used to build upon the functionality of base


interfaces. They allow for further abstraction, code organization, and
provide a more specific contract for implementing classes.
To declare a derived interface in C#, you use
DERIVED the 'interface' keyword followed by the derived
I N T E R FA C E interface name, a colon, and the base interface
D E C L A R AT I O N
name. Here's an example:

public interface IDerivedInterface : IBaseInterface


{
void AdditionalMethod();
}

You might also like