Imagine the interface isettable namedperson interface

Info icon This preview shows pages 159–162. Sign up to view the full content.

What happens if you have conflicting names? Imagine the interface ISettable NamedPerson : interface ISettableNamedPerson { string Name { get; set; } } What happens if we implement both INamedPerson and ISettableNamedPerson on our FirefighterBase ? abstract class FirefighterBase : INamedPerson , ISettableNamedPerson , ISalariedPerson { // ... } The answer is that everything is just fine! Each interface requires that we implement a string property called Name ; one requires at least a getter, the other a getter and a setter. Deriving Interfaces from Other Interfaces | 135
Image of page 159

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

When we access the property through the relevant interface, it can resolve correctly which member we meant; there’s no requirement for a separate implementation for each interface. But what if that was actually wrong? What if our Name property on INamedPerson had entirely different semantics from the one on ISettableNamedPerson ? Let’s suppose that one is intended to allow only letters and numbers with no spaces and the other is just our freeform “any old text” implementation with which we are familiar. Whenever our client expects an INamedPerson we need to provide the second imple- mentation, and whenever the client expects an ISettableNamedPerson , the first. We can do that by explicitly implementing the interfaces . Explicit Interface Implementation To explicitly implement a particular member of an interface, you drop the accessibility modifier and add the interface name as a prefix, as shown in Example 4-24 . Example 4-24. Explicit interface implementation class AFootInBothCamps : INamedPerson , ISettableNamedPerson { private string settableName; string INamedPerson.Name { get { Console.WriteLine("Accessed through the INamedPerson interface" ); return settableName; } } string ISettableNamedPerson.Name { get { return settableName; } set { Console.WriteLine( "Accessed through the " + "ISettableNamedPerson interface"); if( settableName != null && settableName.Contains(" ") ) { // You can't set it if it contains the space // character return; } settableName = value; 136 | Chapter 4: Extensibility and Polymorphism
Image of page 160
} } } Example 4-25 shows how we’re going to access them from our main function. Example 4-25. Calling different interface implementations of the same member name on the same object class Program { static void Main(string[] args) { AFootInBothCamps both = new AFootInBothCamps(); ISettableNamedPerson settablePerson = both; INamedPerson namedPerson = both; settablePerson.Name = "hello"; Console.WriteLine( settablePerson.Name ); Console.WriteLine( namedPerson.Name ); Console.ReadKey(); } } Notice how we’re creating our object, and then providing two additional references to it: one through a variable of type ISettableNamedPerson and one through INamedPerson . We then call on the Name property through each of those interfaces, and get the following output: Accessed through the ISettableNamedPerson interface hello Accessed through the INamedPerson interface hello But what if we try to access it through a reference typed to the class itself?
Image of page 161

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 162
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern