ASP.NET Interview questions and answers for interfaces

ASP.NET Interview questions and answers for  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();
      }
   }
}
In our example we created IBank Customer 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 Implementing 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();
      }
   }
}

Keywords:
interface in c# with example code project
types of interface in c#
interface in c# with real time example
multiple interface in c#
interface in c# interview questions
interface in c# tutorialspoint
difference between abstract class and interface in c#
c# interface property
interface inheritance c#
difference between abstract class and interface in c#
types of interface in c#
c# instantiate interface
interface inheritance vs implementation inheritance
interface with implementation c#
c# struct interface
single inheritance in c#
c# interface tutorial
types of interface in c#
c# interface inheritance override
c# interface constructor
c# interface property
interface in c# with example code project
multiple interface in c#
c# multiple inheritance
can we create instance of interface in c#
can we create instance of interface in java
can we create object of interface in java 8
interface instance c#
many classes can implement the same interface
can we create object for interface
can we create instance of abstract class
a class can implement multiple interfaces
c# interface
tricky question on interface in c#
use of interface in c#
interface in asp net
types of interface in c#
interview questions on inheritance in c# for experienced
user interface in c#
abstract class in c#

No comments: