O'Reilly logo

Learning C# 3.0 by Brian MacDonald, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 13. Interfaces

Back in Chapter 11, you saw how inheritance and abstract methods can dictate the methods that a class has to implement. However, it isn’t always necessary to create a new parent class, even an abstract one, to guarantee the behaviors of your class. For example, you might want to dictate that your class must be storable (capable of being written to disk) or printable. “Storable” isn’t a good candidate for a class, because it doesn’t model an object; instead, it describes a set of behaviors that you want your class to have. Such a description is called an interface. The interface defines the methods that a class must implement, but it doesn’t dictate how the class implements these required methods. This provides a lot of flexibility on the part of the class designer, yet it allows client classes to use those methods with confidence, because the interface methods are guaranteed to be implemented. There are a lot of interesting things you can do with interfaces, including implementing multiple interfaces, combining them, inheriting them, and casting to them. All of it can be tricky to understand at first, so we’ll describe them all thoroughly in this chapter.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required