0% found this document useful (0 votes)
19 views3 pages

Object Oriented Programming

Interfaces can inherit from other interfaces, hiding members defined in the base interface. Classes implementing interfaces can explicitly specify the interface name when implementing members to avoid ambiguity. Explicit implementation makes the member only accessible through an interface reference, not the class directly. This allows implementing the same member signature from multiple interfaces by qualifying with the interface name. The example demonstrates a class implementing two interfaces with the same member name, resolving the conflict by explicitly implementing each version separately.

Uploaded by

Eqra Maqsood
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
19 views3 pages

Object Oriented Programming

Interfaces can inherit from other interfaces, hiding members defined in the base interface. Classes implementing interfaces can explicitly specify the interface name when implementing members to avoid ambiguity. Explicit implementation makes the member only accessible through an interface reference, not the class directly. This allows implementing the same member signature from multiple interfaces by qualifying with the interface name. The example demonstrates a class implementing two interfaces with the same member name, resolving the conflict by explicitly implementing each version separately.

Uploaded by

Eqra Maqsood
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 3

Name Hiding with Interface Inheritance

When one interface inherits another, it is possible to declare a member in the derived interface that hides one defined by the base interface. This happens when a member in a derived interface has the same declaration as one in the base interface. In this case, the base interface name is hidden. This will cause a warning message unless you specify the derived interface member with new.

Explicit Implementations

When implementing a member of an interface, it is possible to fully qualify its name with its interface name. Doing this creates an explicit interface member implementation, or explicit implementation, for short. For example, given

interface Program { int T(int x); } then it is legal to implement Program as shown here: class AnotherProgram : Program { int Program.T(int x) { return x ; } }

As you can see, when the T( ) member of Program is implemented, its complete name, including its interface name, is specified. There are two reasons that you might need to create an explicit implementation of an interface method. First, when you implement an interface method using its fully qualified name, you are providing an implementation that cannot be accessed through an object of the class. Instead, it must be accessed via an interface reference. Thus, an explicit implementation gives you a way to implement an interface method so that it is not a public member of the

324

PartI:TheC#Language

class that provides the implementation. Second, it is possible for a class to implement two interfaces, both of which declare methods by the same name and type signature. Qualifying the names with their interfaces removes the ambiguity from this situation. Lets look at an example of each. The following program contains an interface called Even, which defines two methods, Even( ) and Odd( ), which determine if a number is even or odd. AnotherProgram then implements Even. When it does so, it implements Odd( ) explicitly.

// Explicitly implement an interface member. using System; interface Even { bool Odd(int x); bool Even(int x); } class AnotherProgram : Even { // Explicit implementation. Notice that this member is private // by default. bool Even.Odd(int x)

{ If (x%2) != 0) return true; else return false; } // Normal implementation. public bool Even(int x) { Even o = this; // Interface reference to the invoking object. return !o.Odd(x); } } class Demo { static void Main() { AnotherProgram ob = new AnotherProgram(); bool result; result = ob.Even(4); if(result) Console.WriteLine("4 is even."); // result = ob.Odd(4); // Error, Odd not exposed. // But, this is OK. It creates an Even reference to a AnotherProgram object // and then calls Odd() through that reference. Even iRef = (Even) ob; result = iRef.Odd(3); if(result) Console.WriteLine("3 is odd."); } } PART I Chapter12:Interfaces,Structures,andEnumerat i o n s 325
Since Odd( ) is implemented explicitly, it is not exposed as a public member of AnotherProgram. Instead, Odd( ) can be accessed only through an interface reference. This is why it is invoked through o (which is a reference variable of type Even) in the implementation for Even( ). Here is an example in which two interfaces are implemented and both interfaces declare a method called Meth( ). Explicit implementation is used to eliminate the ambiguity inherent in this situation.

// Use explicit implementation to remove ambiguity. using System; interface ProgramA { int Meth(int x); } interface ProgramB { int Meth(int x); } // AnotherProgram implements both interfaces. class AnotherProgram : ProgramA, ProgramB

{ // Explicitly implement the two Meth()s. int ProgramA.Meth(int x) { return x + x; } int ProgramB.Meth(int x) { return x * x; } // Call Meth() through an interface reference. public int MethA(int x) { ProgramA a_ob; a_ob = this; return a_ob.Meth(x); // calls ProgramA } public int MethB(int x) { ProgramB b_ob; b_ob = this; return b_ob.Meth(x); // calls ProgramB } } class FQIFNames { static void Main() { AnotherProgram ob = new AnotherProgram(); Console.Write("Calling ProgramA.Meth(): "); Console.WriteLine(ob.MethA(3)); 326 P a r t I : T h e C # L a n g u a g e Console.Write("Calling ProgramB.Meth(): "); Console.WriteLine(ob.MethB(3)); } }
The output from this program is shown here:
Calling ProgramA.Meth(): 6 Calling ProgramB.Meth(): 9

Looking at the program, first notice that Meth( ) has the same signature in both ProgramA and ProgramB. Thus, when AnotherProgram implements both of these interfaces, it explicitly implements each one separately, fully qualifying its name in the process. Since the only way that an explicitly implemented method can be called is on an interface reference, AnotherProgram creates two such references, one for ProgramA and one for ProgramB. It then calls two of its own methods, which call the interface methods, thereby removing the ambiguity.

You might also like