Wednesday, August 19, 2009

Interfaces in asp.net and interview questions regarding interfaces

Interfaces can be created in C# with the help of the “interface “keyword. Interface can have only abstract members and these can be events, methods, properties and Indexers. Interface members do 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. Interface only contains member declaration not their implementation. Interface does not contain any declaration of constants, constructors, destructors, static etc members. 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.
Example :
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 IBankCustomer interface. The interface declares 2 methods.
1. void DepositMoney();
2. void WithdrawMoney();

Combining Interfaces

Two or more interfaces can be combined into a single interface and implemented in a class, as shown in Listing 2:
using System;
interface Interdemo
{
void Show();
}

interface Interdemo1
{
void Display();
}

interface Combineinter:Interdemo,Interdemo1
{
//Above interfaces combined
}

class Multipleinterimp:Combineinter
{
public void Show()
{
Console.WriteLine("Show() method Implemented");
}

public void Display()
{
Console.WriteLine("Display() method Implemented");
}

public static void Main(string[] args)
{
Multipleinterimp inter = new Multipleinterimp();
inter.Show();
inter.Display();
}
}

is Operator for C# .Net interfaces

You easily can determine whether a particular interface is implemented in a class by using is and as operators. The is operator enables you to check whether one type or class is compatible with another type or class; it returns a Boolean value.
using System;

interface Interdemo
{
bool Show();
}

interface Interdemo1
{
bool Display();
}

class Interimp:Interdemo
{
public bool Show()
{
Console.WriteLine("Show() method Implemented");
return true;
}

public static void Main(string[] args)
{
Interimp inter = new Interimp();
inter.Show();

if(inter is Interdemo1)
{
Interdemo1 id = (Interdemo1)inter;
bool ok = id.Display();
Console.WriteLine("Method Implemented");
}

else
{
Console.WriteLine("Method not implemented");
}
}
}


As operator in interface:

as operator returns null if there is any incompatibility between types
using System;

interface Interdemo
{
bool Show();
}

interface Interdemo1
{
bool Display();
}

class Interimpas:Interdemo
{
public bool Show()
{
Console.WriteLine("Show() method Implemented");
return true;
}

public static void Main(string[] args)
{
Interimpas inter = new Interimpas();
inter.Show();

Interdemo1 id = inter as Interdemo1;

if(null!=id)
{

bool ok = id.Display();
Console.WriteLine("Method Implemented");
}

else
{
Console.WriteLine("Method not implemented");
}
}
}

Avoiding Name Ambiguity

Suppose you are declaring same method definitions in two different interfaces. The compiler will naturally show an error due to the ambiguity of the implemented method. Even if you use the "is" keyword, the compiler still will show warnings. To avoid this, you have to follow the following syntax as shown:
Void .
{
//Body goes here
}
Following example illustrates the application of the preceding concept in detail:
using System;

interface Interdemo
{
void Show();
}

interface Interdemo1
{
void Show();
}


class Interclash:Interdemo,Interdemo1
{
void Interdemo.Show()
{
Console.WriteLine("Show() method Implemented");
}

void Interdemo1.Show()
{
Console.WriteLine("Display() method Implemented");
}

public static void Main(string[] args)
{
Interclash inter = new Interclash();
inter.Interdemo.Show();
inter.Interdemo1.Show();
}
}

Multiple inheritance using interfaces:

Multiple inheritance is not possible with classes but this can be achieved with the help of the interfaces.

Abstract Class versus Interface

1. A class can implement more than one interface but can only inherit from one abstract class.
2. An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden.
3. An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public. An abstract class can contain access modifiers for the subs, functions, properties.
4. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method where as If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
5. No fields can be defined in interfaces where as an abstract class can have fields and constants defined.

***********Questions****************


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 Implemeting 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();
}
}
}

No comments:

Post a Comment