Multiple Implementation in C#


Can you inherit from multiple classes in C#?Simply put, this cannot be done. However there are ways around it. Froma design perspective you must ask yourself, Will a Class fullyrepresent an object? Meaning that, if we have a base class withabstract methods designed for a particular application and we know thatthe inheriting object will only need the methods defined in that class.We now have a valid design pattern here.

The Vehicle Car Object

Lets say we have an abstract class called"Vehicle" as well as another class called "ConstructionVehicle". Thevehicle class has methods such as Accelerate() , Stop(), and the"ConstructionVehicle" class has methods such as ExecuteDump() andTurnOnBackUpSound(). If we were only going to build a Car object andknow we would only use those methods from the "Automobile" class thiswould be fine.

The DumpTruck Object

Now we want to create another object called"DumpTruck". We could inherit from the Automobile class but that classdoes not have the methods that we need called ExecuteDump() andTurnOnBackUpSound(). If we were using a language such as C++ we couldeasily inherit from both classes using multiple inheritance.However, seeing C# is our language of choice, multiple inheritance isnot an option, you may only inherit from one Base Class.

From Abstract Classes to Interfaces

 

From a design perspective we mustchoose a different design. C# supports what is called "MultipleImplementation", which is to says a class can implement more than oneinterface. Our design now changes the "Vehicle" class and the"ConstructionVehicle" class into interfaces. Below we have defined thetwo interfaces with their very simplistic methods:

ie:

interface IConstructionVehicle
{
void ExecuteDump();
void TurnOnBackUpSound();
}
interface IVehicle
{
void Accelerate();
void Stop();
void TurnOnBackUpSound();
}

If we built a class that inherited from thesetwo interfaces we would be able to do so spanning multiple inheritedinterfaces. Design problem solved! Or is it?

Explicit Interface Implementation

If you look at both interfaces defined aboveyou'll notice that they share in common a method of the same name"TurnOnBackUpSound()". Problem?No, in fact C# supports what is known as "Explicit InterfaceImplementation", which allows the programmer to specify which member ofwhich interface they want to use. Putting the Interface name in frontof the member name allows this to happen as shown below.

public class DumpTruck: IEngine, IBody
{

void IEngine.Test()
{
Console.WriteLine("This is the Engine TEst");
}
void IBody.Test()
{
Console.WriteLine("This is the Body TEst");
}
}

Implementation Hiding

Another benefit to this technique is somethingcalled "Implementation Hiding". Implementation Hiding allows themethods from the implemented interface to be hidden from the derivedclass unless the developer explicitly calls the interface. Thistechnique obviously reduces the clutter for a developer.

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="468" height="60"><param name="movie" value="/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" /><param name="quality" value="high" /> <embed src="http://www.csharphelp.com/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="468" height="60"></embed> </object>

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!