Interfaces



1. What is an Interface ?  

Interface is a contract which guarantees client(consumer) how your classes or structs are going to behave.They consists only method skeleton and no implementations. example- Interface i1 { void Method1(); }


2. Whats the naming convention of interface ?  

The interface name should start with an I.Example--IDisposable,IEnumerable


3. Whats the use of Interface?  

->Interface can be used when you cannot inherit from a class like for structs. ->More than one interface can be inherited thus supporting multiple inheritance in .Net ->Interface helps in defining a common functionality across multiple types. ->Used in plug-n-play architecture.


4. What are the difference between interfaces and abstract classes?  

1) Abstract classes can have implementations for some of its members, but the interface can’t have implementation for any of its members.

2) Interfaces cannot have fields where as an abstract class can have fields.

3) An interface can inherit from another interface only and cannot inherit from any class; where as an abstract class can inherit from another class or another interface.

4) A class can inherit from multiple interfaces at the same time (In this situation C# supports multiple inheritance), where as a class cannot inherit from multiple classes at the same time.

5) Abstract class members can have access modifiers where as interface members cannot have access modifiers.




5. When do you choose interface over an abstract class or vice versa?  

We use abstract class and interface where two or more entities do same type of work but in different ways. Means the way of functioning is not clear while defining abstract class or interface.When functionality of each task is not clear then we define interface. If functionality of some task is clear to us but there exist some functions whose functionality differs object by object then we declare abstractclass.

If we have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface.

When you have an abstract class, you can share implementation for all derived classes in one central place, and avoid code duplication in derived classes.



6. What are the advantages of using interfaces?  

1) Interfaces facilitate parallel application development

2) They are great for implementing Inversion of Control or Dependency Injection.

3) Interfaces enable mocking for better unit testing

4) Interfaces allow us to develop very loosely coupled systems

5) Interfaces also allow us to implement polymorphic behavior



7. If the class does not wish to provide Implementation for all the members inherited from the interface. What can we do?  

The inherited class has to be marked as abstract.



8. What are the benefits of using an interface?  

Implemented interface enforces the class like a standard contract to provide all implementation of interface members.

In architecting the project we can define the proper naming conventions of methods, properties in an interface so that while implementing in a class we use the name conventions.

We can use the "interface" as a pointer in the different layers of applications.

We can use an interface for implementing run time polymorphism.



9. What are various Forms of implementing interface ?  

There are two of ways of implementing interfaces

Explicit
Implicit



10. How can an interface inherit another interface?  

We can inherit an interface by another interface. For example we have two interfaces, one is "IVehicle" and another is "ICart". The IVehicle interface is inherited by the ICart interface and the ICart interface is inherited by the "Vehicle" class. So we need to implement methods of both IVehicle and ICart interfaces in Vehicle classes.
using System;
namespace InterfaceExamples
{
     interfaceIVehicle
    {
        double Speed(int distance, int hours);
    } 
    interfaceICart :IVehicle
   {
        double CalculateDistance(double speed,int hours);
    }
 
     classVehicle : ICart
    {
        publicdouble Speed(int distance, int hours)
        {
            double speed = 0.0d;
            speed = distance / hours;
            return speed;
        }
       publicdouble CalculateDistance(double speed, int hours)
        {
            double distance = 0.0d;
            distance = speed * hours;
            return distance;
        }
    }   
    classProgram
    {
        staticvoid Main(string[] args)
        {
            int distance =500,hours=2;
            double speed,calDistance = 0.0; 
            Vehicle objVehicle = newVehicle();
            speed = objVehicle.Speed(distance, hours);
            Console.WriteLine("speed is {0}", speed);
            calDistance = objVehicle.CalculateDistance(speed, hours);
            Console.WriteLine("calculated distance is {0}", distance);
            Console.Read();
        }       
    }
}

output:-speed is 250
calculated distance is 500



11. Do interface have accessibility modifier.  

All elements in Interface should be public. Therefore, by default all interface elements are public by default.


12. What is an interface class  

Interface is an abstract class which has only public abstract methods and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.


13. Why can’t you specify the accessibility modifier for methods inside the interface?  

In an interface, we have virtual methods that do not have method definition. All the methods are there to be overridden in the derived class. That’s why they all are public.


14. What happens if the inherited interfaces have conflicting method names?  

Implement is up to you as the method is inside your own class. There might be problem when the methods from different interfaces expect different data, but as far as compiler cares you’re okay.


.Net Interview Question

PHP Interview Question

Java Interview Question

AngularJS Interview Questions