C# Interview Questions related to Interfaces.

Explain what is an Interface in C#?
An Interface in C# is created using the interface keyword. An example is shown below.

using System;
namespace Interfaces
{
interface IBankCustomer
{
void DepositMoney();
void WithdrawMoney();
}
public class Demo : IBankCustomer
{
public void DepositMoney()
{
Console.WriteLine("Deposit Money");
}

public void WithdrawMoney()
{
Console.WriteLine("Withdraw Money");
}

public static void Main()
{
Demo DemoObject = new Demo();
DemoObject.DepositMoney();
DemoObject.WithdrawMoney();
}
}
}

Interfaces Video

Click here for video on Explicit Interface Implementation
In our example we created IBankCustomer interface. The interface declares 2 methods.
1. void DepositMoney();
2. void WithdrawMoney();

Notice that method declarations does not have access modifiers like public, private, etc. By default all interface members are public. It is a compile time error to use access modifiers on interface member declarations. Also notice that the interface methods have only declarations and not implementation. It is a compile time error to provide implementation for any interface member. In our example as the Demo class is inherited from the IBankCustomer interface, the Demo class has to provide the implementation for both the methods (WithdrawMoney() and DepositMoney()) that is inherited from the interface. If the class fails to provide implementation for any of the inherited interface member, a compile time error will be generated. Interfaces can consist of methods, properties, events, indexers, or any combination of those four member types. When a class or a struct inherits an interface, the class or struct must provide implementation for all of the members declared in the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation.

Can an Interface contain fields?
No, an Interface cannot contain fields.

What is the difference between class inheritance and interface inheritance?
Classes and structs can inherit from interfaces just like how classes can inherit a base class or struct. However there are 2 differences.
1. A class or a struct can inherit from more than one interface at the same time where as A class or a struct cannot inherit from more than one class at the same time. An example depicting the same is shown below.

using System;
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
interface Interface2
{
void Interface2Method();
}
class BaseClass1
{
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1 Method");
}
}
class BaseClass2
{
public void BaseClass2Method()
{
Console.WriteLine("BaseClass2 Method");
}
}

//Error : A class cannot inherit from more than one class at the same time
//class DerivedClass : BaseClass1, BaseClass2
//{
//}

//A class can inherit from more than one interface at the same time
public class Demo : Interface1, Interface2
{
public void Interface1Method()
{
Console.WriteLine("Interface1 Method");
}

public void Interface2Method()
{
Console.WriteLine("Interface2 Method");
}

public static void Main()
{
Demo DemoObject = new Demo();
DemoObject.Interface1Method();
DemoObject.Interface2Method();
}
}
}

2. When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations.

Can an interface inherit from another interface?
Yes, an interface can inherit from another interface. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface one time, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members.

Can you create an instance of an interface?
No, you cannot create an instance of an interface.

If a class inherits an interface, what are the 2 options available for that class?
Option 1: Provide Implementation for all the members inheirted from the interface.

namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}

class BaseClass1 : Interface1
{
public void Interface1Method()
{
Console.WriteLine("Interface1 Method");
}
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1 Method");
}
}
}

Option 2: If the class does not wish to provide Implementation for all the members inheirted from the interface, then the class has to be marked as abstract.

namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}

abstract class BaseClass1 : Interface1
{
abstract public void Interface1Method();
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1 Method");
}
}
}

A class inherits from 2 interfaces and both the interfaces have the same method name as shown below. How should the class implement the drive method for both Car and Bus interface?
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}

class Demo : Car,Bus
{
//How to implement the Drive() Method inherited from Bus and Car
}
}

To implement the Drive() method use the fully qualified name as shown in the example below. To call the respective interface drive method type cast the demo object to the respective interface and then call the drive method.

using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}

class Demo : Car,Bus
{
void Car.Drive()
{
Console.WriteLine("Drive Car");
}
void Bus.Drive()
{
Console.WriteLine("Drive Bus");
}

static void Main()
{
Demo DemoObject = new Demo();
((Car)DemoObject).Drive();
((Bus)DemoObject).Drive();
}
}
}

What do you mean by "Explicitly Implemeting an Interface". Give an example?
If a class is implementing the inherited interface member by prefixing the name of the interface, then the class is "Explicitly Implemeting an Interface member". The disadvantage of Explicitly Implemeting an Interface member is that, the class object has to be type casted to the interface type to invoke the interface member. An example is shown below.

using System;
namespace Interfaces
{
interface Car
{
void Drive();
}

class Demo : Car
{
// Explicit implementation of an interface member
void Car.Drive()
{
Console.WriteLine("Drive Car");
}

static void Main()
{
Demo DemoObject = new Demo();

//DemoObject.Drive();
// Error: Cannot call explicitly implemented interface method
// using the class object.
// Type cast the demo object to interface type Car
((Car)DemoObject).Drive();
}
}
}

28 comments:

  1. i think structure in C# doesn't support inheritance.you have specified here inheritance concept with struct keyword in theortical point of view...

    ReplyDelete
  2. Hello,

    C# Supports interface but doesn't support multiple inheritance.

    ReplyDelete
  3. C# supports multiple inheritance using interfaces,

    it's good theory where i found it

    ReplyDelete
  4. C# supports multiple inheritance for interfaces, single inheritance for classes and no inheritance for structs

    ReplyDelete
  5. What Is The Difference Between Abstract Clases and Interfaces.when to use them

    ReplyDelete
    Replies
    1. When to use Interface:
      If your child classes should implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.

      When to use Abstract Class:
      When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.

      Delete
  6. hey..thanks..really nice and vry helpful blog :)
    hav one qsn..
    suppose we hav one interface say I and one abstract class say A..then can class inherit both..interface and abstract class at the same time..
    class ABC:I,A or
    class ABC:A,I..
    is it possible??

    ReplyDelete
  7. re: amrita

    yes a class can inherit the abstract class and interface at the same time

    ReplyDelete
  8. can u give me real life example where we use inteface and abstract class which situation we use interface and which situation we use abstract class

    ReplyDelete
  9. Can you provide the WebServices questions

    ReplyDelete
  10. interface contains abstract methods only where abstact class can contain both methods

    ReplyDelete
  11. yes a class can inherit the abstract class and interface at the same time

    Jegan

    namespace ConsoleApplication3
    {
    interface car
    {
    void Drivecar();
    }
    interface bus:car
    {
    void Drivebus();
    }
    abstract class abcls
    {
    public void abstrmetod()
    {
    Console.WriteLine("This is first method ");
    }
    public abstract void method1();
    }
    class Demo : abcls,bus
    {
    public void Drivecar()
    {
    Console.WriteLine("Drivecar");
    }
    public void Drivebus()
    {
    Console.WriteLine("drivebus");
    }
    public override void method1()
    {
    Console.WriteLine("This is abstract method");
    }
    static void Main()
    {
    Demo obj = new Demo();
    obj.Drivecar();
    obj.Drivebus();
    obj.method1();
    obj.abstrmetod();
    Console.Read();
    }
    }
    }

    ReplyDelete
  12. A class implements a interface and inherits a abstract class. Implements and interface because in an interface you define the methods properties ...generally for consistency and no code is written in a interface....where as in an abstract class you have code written which can be inherited and reused to provide complete implementation

    ReplyDelete
  13. can we inherits two abstract classes at the same time???????

    ReplyDelete
  14. a class can inherit only one Class.........

    ReplyDelete
  15. Yes, a class can implement an interface and inherit from an abstract class(even a base class) at a same line, the only condition is base class comes first before any interfaces...
    e.g. class Base1:abstClass, Iinterface
    {
    ...........
    ...........
    }

    ReplyDelete
  16. We can't inherit multiple class not even abstract class in C#.Multiple inheritance is not possible in C#.

    ReplyDelete
  17. I've a very simple question ? why & where we need to use interfaces? Is that because of the security in multi tier apps, we use them?

    ReplyDelete
    Replies
    1. No not for security but for implementation if you inherit an interface you must have to provide implementation for all the method which are declare in that interface otherwise compiler error will occur.

      Delete
  18. What is the difference between interface and abstract class?

    ReplyDelete
  19. You can define methods in abstract class but not in interface.All the variable in interface are public and final by default.

    ReplyDelete
  20. why do you use interfaces. Is it for not exposing the methods directly ?

    ReplyDelete
  21. Multiple inheritance in C# can be done by interface.

    In abstract class one can have concrete method too with abstract method but in interface all method must be incomplete.

    ReplyDelete
  22. how to implement the void Method() of the interface in the following case ?

    class B {
    public void Method() {
    // some code here
    //...
    }
    }

    interface I {
    void Method();
    }

    class D : B, I {
    // how to implement the void Method() of the interface
    // public void I.Method() { ... }
    }

    ReplyDelete
  23. We use explicit interface implementation technique to implement the void Method() of the interface. Please refer to the article below.
    Explicit Interface Implementation

    ReplyDelete
  24. sir in your c# tutorial of interface you said that interface can have delegate but they are type than please let me know how can we declare delegate in interface and when?
    thanks santosh

    ReplyDelete
  25. in interface can we declare a property and implement it with some class? but How..............................
    thank u Hanan Shawal

    ReplyDelete
    Replies
    1. Interface is incomplete that means we can declare method only. interface method can only implement in Derived class. Also not possible to declare property, it will throw error.

      Delete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us