Wednesday 23 March 2011

Interface Interview Questions

What is an Interface?

An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.

Important point:

a. By default it is public. We cannot specify any access specifier in interface.
b. It doesnot contain implementation of method.
[code]
using System;
namespace Interfaces
{
interface Icustdetails
{
void GetName();
void GetID();
}
public class Demo : Icustdetails
{
public void GetName()
{
Console.WriteLine("Gives the name");
}

public void GetID()
{
Console.WriteLine("gives the ID");
}

public static void Main()
{
Demo obj_demo = new Demo();
demo_obj.GetName();
demo_obj.GetID();
}
}
}

[/code]
In this example we have declared an interface ICustdetails with the abstract methods GetName and GetID. Now their is a class demo
which is inherited from Icustdetails so,it has to provide the implementation of these methods. If it doesn't provide the implementation
then compile time error is thrown.

What do you mean by "Explicitly Implemeting an Interface"?

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". 

Can we declare private methods inside an Interface?

No, they all must be public, you are not allowed to specify any accessibility, its public by default.

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

A class inherits from 2 interfaces and both the interfaces have the same method name as shown below.
How should the class implement the GetName method for both IOrderdetails and ICustdetails interface?

[code]
  interface ICustdetails
    {
         void GetName(int n);
    }
    interface IOrderdetails
    {
        void GetName(int n);
    }
    class Orders : ICustdetails, IOrderdetails
    {
       //how we implement method of both interface
    }
[/code]

To implement the GetName() method we use the fully qualified name as shown below.
To call the respective interface GetMethod method type cast the Orders object to the respective interface and then call the  method.
[code]
 interface ICustdetails
    {
         void GetName(int n);
    }
    interface IOrderdetails
    {
        void GetName(int n);
    }
    class Orders : ICustdetails, IOrderdetails
    {
        void ICustdetails.GetName(int n)
        {
           
        Console.WriteLine("These are customer details");
          
        }
        void IOrderdetails.GetName(int n)
        {           
            Console.WriteLine("These are order details");
          
        }
    }
 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter customer number");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter order number");
            int o = int.Parse(Console.ReadLine());
            Orders obj = new Orders();
            ICustdetails cd = obj;
            IOrderdetails od = obj;
            cd.GetName(n);
            od.GetName(o);
            Console.ReadLine();

        }
    }

[/code]

If a class inherits an interface, what are the options available for that class?
Their are 2 options available in this case:

a. A class has to implement all the members of that interface.

namespace demo_interface
{
interface Bank
{
void savingaccount();
Void currentaccount();
}

class bankdetail : Bank
{
public void savingaccount()
{
Console.WriteLine("savingaccount Method");
}
public void currentaccount()
{
Console.WriteLine("currentaccount Method");
}
}
}

Option 2: class must use the abstract keyword

namespace demo_interface
{
interface Bank
{
void savingaccount();
Void currentaccount();
}


abstract class bankdetail : Bank
{
abstract public void savingaccount();
abstract public void savingaccount();
public void bankdetailmethod()
{
Console.WriteLine("Bankdetail Method");
}
}
}



No comments:

Post a Comment