An interface in C# is defined using the interface keyword. It serves as a blueprint that declares methods, properties, events or indexers without implementation. A class or struct that implements the interface must provide the actual implementation, either implicitly or explicitly.
There are some important points to remember, as mentioned below:
- Interfaces cannot have private members.
- By default, all the members of Interface are public and abstract.
- The interface will always be defined with the help of the keyword interface.
- Interfaces cannot contain fields because they represent an implementation of data.
- We can achieve multiple inheritance with the interface but not with the classes.
- We can implement multiple interfaces in a single class separated by commas.
Example 1: Demonstrating the basic case for implementing the Interface.
C#
// Demonstrate working of Interface
using System;
interface inter1{
// method having only declaration with no definition
void display();
}
// Implementing inteface in Geeks
class Geeks : inter1{
// providing the body part of function
public void display(){
Console.WriteLine("Demonstration of interface");
}
public static void Main(String[] args){
Geeks t = new Geeks();
t.display();
}
}
OutputDemonstration of interface
Example 2: Implementation of interfaces and the use of polymorphism in C#
C#
using System;
// interface declaration
interface Vehicle{
// abstract method.
void speedUp(int a);
}
// class implements interface
class Bicycle : Vehicle{
int speed;
// Method increase speed
public void speedUp(int increment){
speed = speed + increment;
}
// Method check speed
public void CheckSpeed(){
Console.WriteLine("speed: " + speed);
}
}
// class implements interface
class Bike : Vehicle{
int speed;
// to increase speed
public void speedUp(int increment){
speed = speed + increment;
}
public void CheckSpeed(){
Console.WriteLine("speed: " + speed);
}
}
class Geeks
{
public static void Main(String[] args)
{
// creating an instance of Bicycle doing some operations
Bicycle bicycle = new Bicycle();
bicycle.speedUp(3);
Console.WriteLine("Bicycle present state :");
bicycle.CheckSpeed();
// creating instance of bike.
Bike bike = new Bike();
bike.speedUp(4);
Console.WriteLine("Bike present state :");
bike.CheckSpeed();
}
}
OutputBicycle present state :
speed: 3
Bike present state :
speed: 4
Advantages of Interfaces
- Loose coupling: It is used to achieve loose coupling rather than concrete implementations, we can decouple classes and reduce interdependencies.
- Abstraction: Interface helps to achieve full abstraction.
- Maintainability: It helps to achieve component-based programming that can make code more maintainable.
- Multiple Inheritance: Interface used to achieve multiple inheritance and abstraction.
- Define architecture: Interfaces add a plug and play like architecture into applications.
- Modularity: Promote a cleaner and more modular design. By separating the definition of behavior from the implementation
Abstract Class vs Interface
In C#, both abstract classes and interfaces are used to define contracts and enable polymorphism, but they differ in implementation capabilities and usage. Abstract classes can provide both behavior and declarations, while interfaces primarily define a contract that implementing classes must follow.
| Feature | Abstract Class | Interface |
|---|
| Implementation | Can have both method declarations and implementations | Only method/property/event/indexer declarations (except default implementations in C# 8.0+) |
|---|
| Access Modifiers | Can have private, protected, public members | Members are public by default; cannot have private fields |
|---|
| Fields | Allowed | Not allowed |
|---|
| Constructors | Allowed | Not allowed |
|---|
| Inheritance | Single inheritance | Multiple inheritance allowed |
|---|
| Use Case | Shared behavior across related classes | Contract or capability for multiple unrelated classes |
|---|
In C#, which of the following is NOT true about interfaces?
-
All members are public and abstract by default
-
Interfaces can contain fields
-
Interfaces are defined using the interface keyword
-
A class can implement multiple interfaces
Explanation:
Interfaces cannot contain fields, only method, property, event, or indexer declarations. All members are implicitly public and abstract.
Which of the following is a disadvantage of using abstract classes over interfaces in C#?
-
Abstract classes support constructors
-
Abstract classes support encapsulation
-
Abstract classes do not allow multiple inheritance
-
Abstract classes allow code reuse
Explanation:
Abstract classes do not support multiple inheritance in C#, which is a limitation compared to interfaces. Interfaces allow multiple inheritance.
Which of the following is possible in an abstract class but NOT in an interface (before C# 8.0)?
-
Declaring methods without implementation
-
Using multiple inheritance
-
-
Declaring only public members
Explanation:
Abstract classes can contain constructors, while interfaces (before C# 8.0) cannot. Interfaces can only contain method declarations, not constructors.
What new feature did C# 8.0 introduce in interfaces?
-
Interfaces can now contain fields
-
Interfaces can now contain constructors
-
Interfaces can now contain methods with implementation
-
Interfaces can now be instantiated
Explanation:
C# 8.0 introduced default interface methods, which allow interfaces to provide implementations without breaking existing classes.
In C# 8.0, why can’t a class object call a default method of an interface directly (e.g., t.display_2())?
-
Because default methods are private
-
Because default methods can only be called through an interface reference
-
Because default methods require the static keyword
-
Because default methods must be overridden
Explanation:
Default methods in interfaces can only be accessed through an interface reference, not directly via the class object.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers