Notice also that we dropped the abstract modifier on

Info icon This preview shows pages 157–160. Sign up to view the full content.

View Full Document Right Arrow Icon
Notice also that we dropped the abstract modifier on the members; an interface is implicitly without implementation. There are no accessibility modifiers on the mem- bers either; an interface member is only ever allowed to be public . The only other change we’ve made is to prefix our interface name with an I . This is not a rule, but another one of those naming conventions to which most people conform. We can now implement those interfaces on our Administrator , as shown in Exam- ple 4-21 . Example 4-21. Implementing interfaces class Administrator : INamedPerson , ISalariedPerson { public decimal Salary { get; set; } public string Name { get { StringBuilder name = new StringBuilder(); AppendWithSpace(name, Title); AppendWithSpace(name, Forename); AppendWithSpace(name, Surname); return name.ToString(); } } // ... } C# Supports Multiple Inheritance of Interface | 133
Image of page 157

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

View Full Document Right Arrow Icon
And we can implement them on our FirefighterBase , as shown in Example 4-22 . Example 4-22. The same interfaces in a different part of the class hierarchy abstract class FirefighterBase : INamedPerson , ISalariedPerson { public string Name { get; set; } public decimal Salary { get; set; } // ... } Notice that we can happily implement the setter on our FirefighterBase , even though the interface only requires a getter. The restrictions on how you implement the interface—as long as you conform to the contract it specifies—are much looser than those on overrides of a base class. Also, C# doesn’t allow you to use the simple property syntax to define virtual properties or their overrides, but there is no such restriction when you’re implementing an interface. So we’ve been able to use simple property syntax here rather than having to implement using full-blown properties. We can now make use of this interface in our FireStation class. Instead of a list of objects, we can use a list of INamedPerson , and call on the Name property in our RollCall method, as shown in Example 4-23 . Example 4-23. Modifying the FireStation class to use an interface class FireStation { List<INamedPerson> clockedInStaff = new List<INamedPerson>(); public void ClockIn(INamedPerson staffMember) { if (!clockedInStaff.Contains(staffMember)) { clockedInStaff.Add(staffMember); Console.WriteLine("Clocked in {0}", staffMember.Name); } } public void RollCall() { foreach (INamedPerson staffMember in clockedInStaff) { Console.WriteLine(staffMember.Name); 134 | Chapter 4: Extensibility and Polymorphism
Image of page 158
} } } If you’ve been following through the code in Visual Studio (which I thoroughly recommend), you’ll also need to change your object initial- izers back to this form: Firefighter joe = new Firefighter { Name = "Joe" }; If we compile and run, we get the output we hoped for—a roll call of everyone in the station: Clocked in Joe Clocked in Bill Clocked in Harry Clocked in Mr Arthur Askey Joe Bill Harry Mr Arthur Askey Deriving Interfaces from Other Interfaces Interfaces support inheritance too, just like classes. If you want, you could create a named, salaried person interface like this: interface INamedSalariedPerson : INamedPerson , ISalariedPerson { } What happens if you have conflicting names? Imagine the interface
Image of page 159

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

View Full Document Right Arrow Icon
Image of page 160
This is the end of the preview. Sign up to access the rest of the document.

{[ 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