Interface

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.Why Interface methods cannot have access modifiers ?
 
Interface methods are always public by default.

5.Can Interface have properties in them ?
 yes.a property in interface is defined as below
Interface I1
{
   int prop
   {
      get;
      set;
    }
}
get and set cannot have any body.
6.Can interface have fields ?
 No

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

8.why is it better to access interface through interface reference only ?
9.Give a real world example of Interface ? 
10.Whats the difference between Interface and Abstract Class ?
 

Abstract Class Interface
Can have implementations for some of its methods Only method declaration
A class can inherit only one abstract class Can inherit many interfaces. Provides way of implementing multiple inheritance in .Net
Abstract class are fast Interface are slow
Can contain fields, constructor etc Cannot contain any of these
Abstract class is of a class type.it need to be derived Interface is interface type.It needs to be implemented
Cannot be inherited by structs Can be inherited by structs



11.When should we Abstract Class and Interface?    
1.  If multiple versions of the component are being created and they are reused  then  use Abstract Class as they simplify versioning.By updating base class all the 

derived  classes gets updated. Whearas if a new version of interface is required it 
has to be created from scratch.
2. If the functionality is used across wide range then use Interface. Abstract Classes should be used for closely related objects.
3. Use interface if there are atleast 2 diff implementations available.
4. If you want to provide common functionality among all the implemented classes go for abstract classes.


12.what is explicit implementation. when is it used?
When a class which has inherited a interface explicitly implements methods of         
that  class is called explicit implementation.This method can now be accessed 
only through interface instance.They are useful in cases when a class inherits
two interfaces and both of  them have same method name.
      Example --
        interface ISimpleCalculator
        {
            void Add();
        }
        interface IComplexCalculator
        {
            void Add();
        }
        class Calculator: ISimpleCalculator,IComplexCalculator
        {
            //This is explicit implementation
            void ISimpleCalculator.Add()
            {
                //doSomething
            }
            void IComplexCalculator.Add() 
           {
                //doSomething
            }
        }

Accesing these interface--

           Calculator obj = new Calculator();
            ISimpleCalculator i1 = new Calculator();
            i1.Add(); //will call ISimpleCalculator.Add() 


You cannot access interface methods from class reference.
             obj.Add()  //will give compilation error.


13.Why public modifier not used in explicit implementation? 

The methods which are explicitly implemented are tied to a interface. and it can be 
accessed  only through interface reference.

14.What is implicit implementation ?  
interface ISimpleCalculator
        {
            void Add();
        }      

        class Calculator: ISimpleCalculator
        {
            //This is implicit implementation
            public void Add()
            {
                //doSomething
            }          
        }

       Add() can be access be directly class reference

15.Does structure support Inheriting from an interface ?
Yes

16.When a class A inherits from an interface I1 and another class B what should  be taken care of ?

      The class being inherited should be the base class
     example- This is how it should  be inherited

            Class A:Class B,I1   

            {

            }


       This is wrong

           Class A:I1,Class B

           {

           }


17.Can a method derived from interface marked as virtual ? 
Yes and they can then be overridden

18.Why cannot you have static method in Interface? 
Because Interface methods have implementation and also can be overriden . Thera is no point in having  static methods as they should have a body and cannot be overriden.

19.Can Interface inherit another interface ?
 Yes

20.Can Interface inherit another class ?
 
Interface cannot inherit another class.

21.Can Interface have access modifier ? 
 Yes.

22.Mention some Interface in .Net Framework?
IDisposable,IEnumerable,ICollection

 








6 comments:

  1. Very helpful article. Great points in the differences table

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in dot net programming your post help me a lot.Thanks for your informative article.Dot Net training in chennai | dot net training and placement | Dot Net training in velachery

    ReplyDelete