Multiple implementation in C #

Source: Internet
Author: User
Tags abstract
Multiple implementation in C #
by Craig Breakspear
Can you inherit from multiple classes in C #? Simply put, this cannot is done. However There are ways around it. From the A design perspective your must ask yourself, would a Class fully represent an object? Meaning that, if we have a base class with abstract methods designed for a particular application and we know this inh Eriting object would only need the methods defined into that class. We now have have a valid design.

The Vehicle car Object

Lets say we have an abstract class called "Vehicle" as OK as another class called "Constructionvehicle". The vehicle class has methods such as accelerate (), Stop (), and the "Constructionvehicle" class has methods as such Tedump () and Turnonbackupsound (). If we were only going to builds a car object and know we would only use those methods the ' automobile ' class this Woul D be fine.

The Dumptruck Object

Now we want to create another object called "Dumptruck". We could inherit from the automobile class but this class does not have the methods that we need called executedump () and Turnonbackupsound (). If we were using a language such as C + + we could easily inherit from both classes using multiple. However, seeing C # are our language of choice, multiple inheritance are not a option, you could only inherit from one Base Cl Ass.

From the Abstract Classes to interfaces


From the design perspective we must choose a different. C # supports what is called ' multiple implementation ', which is to says a class can implement more than one interface. Our design now changes the "Vehicle" class and the "Constructionvehicle" class into interfaces. Below we have defined the two 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 two interfaces we would is able to does so spanning multiple inherited Ces. Design Problem solved! Or is it?
Explicit Interface Implementation

If look at both interfaces defined above you'll notice that they share in common a method of the same name "Turnonback Upsound () ". Problem? No, in fact C # supports what is known as "Explicit Interface implementation", which allows the programmer to specify which Member of which interface they want to use. Putting the Interface name in front of the ' member name allows ' to happen as shown.

public class Dumptruck:iengine, Ibody
{

void Iengine.test ()
{
Console.WriteLine ("This is the Engine TEst");
}
void Ibody.test ()
{
Console.WriteLine ("The Body TEst");
}
}

Implementation hiding
Another benefit to the technique is something called "Implementation hiding". Implementation hiding allows the methods from the implemented interface to be hidden the derived class unless the dev Eloper explicitly calls the interface. This is technique obviously reduces the clutter for a developer.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.