Overriding the Read method for Document! Note Write Method for IStorable in Visual C#.NET

Maker Code 128A in Visual C#.NET Overriding the Read method for Document! Note Write Method for IStorable

Overriding the Read method for Document! Note Write Method for IStorable
Drawing Code 128C In Visual C#
Using Barcode maker for .NET framework Control to generate, create Code 128A image in .NET applications.
www.OnBarcode.com
Reading ANSI/AIM Code 128 In Visual C#
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Explicit Interface Implementation
Make UPC-A In Visual C#
Using Barcode generation for .NET Control to generate, create UPCA image in .NET framework applications.
www.OnBarcode.com
Barcode Maker In C#.NET
Using Barcode printer for .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
In the implementation shown so far, the class that implements the interface (Document) creates a member method with the same signature and return type as the method detailed in the interface. It is not necessary to explicitly state that Document is implementing IStorable, for example; the compiler understands this implicitly. What happens, however, if the class implements two interfaces, each of which has a method with the same signature This might happen if the class implements interfaces defined by two different organizations or even two different programmers. The next example creates two interfaces: IStorable and ITalk. ITalk implements a Read( ) method that reads a book aloud. Unfortunately, this conflicts with the Read( ) method in IStorable. Because both IStorable and ITalk have a Read( ) method, the implementing Document class must use explicit implementation for at least one of the methods. With explicit implementation, the implementing class (Document) explicitly identifies the interface for the method:
Code 39 Extended Generator In Visual C#.NET
Using Barcode printer for Visual Studio .NET Control to generate, create Code 39 image in VS .NET applications.
www.OnBarcode.com
Printing Barcode In Visual C#
Using Barcode maker for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
void ITalk.Read( )
QR Code ISO/IEC18004 Creator In Visual C#
Using Barcode drawer for Visual Studio .NET Control to generate, create QR Code 2d barcode image in .NET framework applications.
www.OnBarcode.com
ISBN - 10 Creator In Visual C#.NET
Using Barcode printer for .NET Control to generate, create International Standard Book Number image in .NET framework applications.
www.OnBarcode.com
Marking the Read( ) method as a member of the ITalk interface resolves the conflict between the identical Read( ) methods. There are some additional aspects you should keep in mind.
ANSI/AIM Code 128 Generation In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create Code 128 Code Set B image in .NET framework applications.
www.OnBarcode.com
USS Code 128 Drawer In Objective-C
Using Barcode printer for iPad Control to generate, create Code 128A image in iPad applications.
www.OnBarcode.com
Explicit Interface Implementation |
DataMatrix Printer In Java
Using Barcode drawer for BIRT Control to generate, create ECC200 image in Eclipse BIRT applications.
www.OnBarcode.com
Code 128 Code Set A Maker In Java
Using Barcode maker for Android Control to generate, create Code 128 Code Set C image in Android applications.
www.OnBarcode.com
First, the explicit implementation method cannot have an access modifier:
Print 2D Barcode In Java
Using Barcode encoder for Java Control to generate, create 2D image in Java applications.
www.OnBarcode.com
Generating EAN13 In Objective-C
Using Barcode maker for iPhone Control to generate, create European Article Number 13 image in iPhone applications.
www.OnBarcode.com
void ITalk.Read( )
UPCA Printer In .NET
Using Barcode printer for .NET Control to generate, create UPC-A Supplement 2 image in Visual Studio .NET applications.
www.OnBarcode.com
Code128 Maker In Objective-C
Using Barcode creator for iPhone Control to generate, create Code-128 image in iPhone applications.
www.OnBarcode.com
This method is implicitly public. In fact, a method declared through explicit implementation cannot be declared with the abstract, virtual, override, or new keyword, either. Most importantly, you cannot access the explicitly implemented method through the object itself. When you write:
European Article Number 13 Drawer In None
Using Barcode creation for Software Control to generate, create EAN13 image in Software applications.
www.OnBarcode.com
UPC Code Recognizer In Visual Basic .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
theDoc.Read( );
Reading PDF 417 In .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Read Code 128 Code Set B In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
the compiler assumes you mean the implicitly implemented interface for IStorable. The only way to access an explicitly implemented interface is through a cast to the interface:
ITalk itDoc = theDoc as ITalk; if (itDoc != null) { itDoc.Read( ); }
Explicit implementation is demonstrated in Example 13-6. Note that there is no need to use explicit implementation with the other method of ITalk:
public void Talk( )
Because there is no conflict, this can be declared as usual.
using using using using System; System.Collections.Generic; System.Linq; System.Text;
namespace Example_13_6_ _ _ _Explicit_Interfaces { interface IStorable { void Read( ); void Write( ); } interface ITalk { void Talk( ); void Read( ); } // Modify Document to also implement ITalk public class Document : IStorable, ITalk {
|
13: Interfaces
// the document constructor public Document(string s) { Console.WriteLine("Creating document with: {0}", s); } // Implicit implementation public virtual void Read( ) { Console.WriteLine("Document Read Method for IStorable"); } public void Write( ) { Console.WriteLine("Document Write Method for IStorable"); } // Explicit implementation void ITalk.Read( ) { Console.WriteLine("Implementing ITalk.Read"); } public void Talk( ) { Console.WriteLine("Implementing ITalk.Talk"); } } class Tester { public void Run( ) { // Create a Document object Document theDoc = new Document("Test Document"); IStorable isDoc = theDoc as IStorable; if (isDoc != null) { isDoc.Read( ); } // Cast to an ITalk interface ITalk itDoc = theDoc as ITalk; if (itDoc != null) { itDoc.Read( ); } theDoc.Read( ); theDoc.Talk( ); }
Explicit Interface Implementation |
static void Main( ) { Tester t = new Tester( ); t.Run( ); } } }
The output looks like this:
Creating document with: Test Document Document Read Method for IStorable Implementing ITalk.Read Document Read Method for IStorable Implementing ITalk.Talk
The first thing the program does is create an IStorable reference to a Document. Then it invokes the Read( ) method of IStorable:
IStorable isDoc = theDoc as IStorable; if (isDoc != null) { isDoc.Read( ); }
Then you cast the document to an ITalk interface, and invoke the Read( ) method of ITalk:
ITalk itDoc = theDoc as ITalk; if (itDoc != null) { itDoc.Read( ); }
The output shows that both of these calls work as expected. The next thing you do is to call the Read( ) and Talk( ) methods directly on the Document:
theDoc.Read( ); theDoc.Talk( );
As you can see in the output, the Read( ) method defaults to the version from IStorable, because that version is implicit. The Talk( ) method is the version from ITalk, because that s the only interface here with a Talk( ) method.
Summary
An interface is a contract through which a class guarantees that it will implement certain methods, provide certain properties and indexers, and support certain events, all of which are specified in the interface definition.
|
Copyright © OnBarcode.com . All rights reserved.