Free mag vol1 | Page 361

CHAPTER 8  WORKING WITH INTERFACES  Source Code The InterfaceNameClash project is located under the Chapter 8 subdirectory. Designing Interface Hierarchies Interfaces can be arranged in an interface hierarchy. Like a class hierarchy, when an interface extends an existing interface, it inherits the abstract members defined by the parent(s). Of course, unlike classbased inheritance, derived interfaces never inherit true implementation. Rather, a derived interface simply extends its own definition with additional abstract members. Interface hierarchies can be useful when you want to extend the functionality of an existing interface without breaking existing code bases. To illustrate, create a new Console Application named InterfaceHierarchy. Now, let’s design a new set of rendering-centric interfaces such that IDrawable is the root of the family tree: public interface IDrawable { void Draw(); } Given that IDrawable defines a basic drawing behavior, we could now create a derived interface that extends this interface with the ability to render in modified formats. For example: public interface IAdvancedDraw : IDrawable { void DrawInBoundingBox(int top, int left, int bottom, int right); void DrawUpsideDown(); } Given this design, if a class were to implement IAdvancedDraw, it would now be required to implement each and every member defined up the chain of inheritance (specifically, the Draw(), DrawInBoundingBox(), and DrawUpsideDown() methods): public class BitmapImage : IAdvancedDraw { public void Draw() { Console.WriteLine("Drawing..."); } public void DrawInBoundingBox(int top, int left, int bottom, int right) { Console.WriteLine("Drawing in a box..."); } } public void DrawUpsideDown() { Console.WriteLine("Drawing upside down!"); } 299