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

 








26 comments:

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

    ReplyDelete
  2. Can Interface have access modifier ?

    ReplyDelete
    Replies
    1. Interface can have access modifier where interface members cant since they are public by default.

      Delete
  3. 21.Can Interface have access modifier ?
    I think its answer is NO because interface is by default is public and its member as well.

    ReplyDelete
  4. learnt so many things from this article. Thanks.

    ReplyDelete
  5. 21.Can Interface have access modifier ?
    This one is wrong answer. can't access modifier of interface.
    Please correct this.

    ReplyDelete
  6. Very Helpful, Thanks for sharing

    ReplyDelete
  7. An Interface may have access modifies but not it's member.For example.

    public interface IInterface
    {
    void Test();

    }


    internal interface IInterface
    {
    void Test();

    }

    ReplyDelete
  8. Nice concept Easley to understand.

    ReplyDelete
  9. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. AngularJS Online Course

    ReplyDelete
  10. Thanks for sharing... Very nice article and easy to understand!!!

    ReplyDelete
  11. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
    Best PHP Training Institute in Chennai|PHP Course in chennai

    Best .Net Training Institute in Chennai
    Oracle DBA Training in Chennai
    RPA Training in Chennai
    UIpath Training in Chennai

    ReplyDelete
  12. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Java Training in Chennai

    Java Training in Velachery

    Java Training inTambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar

    ReplyDelete
  13. Great blog the content is informative and engaging. Visit my website to get best Information
    Frozen Shoulder Treatment in Surrey

    ReplyDelete
  14. Wonderful blog with great piece of information. Visit my website to get best Information
    vintage oval mirror

    ReplyDelete