12.07.2015 Views

Accelerated

Accelerated

Accelerated

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

56CHAPTER 4 ■ CLASSES, STRUCTS, AND OBJECTS■Note One more important note is in order for those used to coding in C++: struct members are private bydefault, just like in class definitions, whereas they are public by default in C++.Lastly, members of interfaces, which I describe fully in Chapter 5, and enums, which I covered inChapter 3, are implicitly public by their very nature. Interfaces are meant to define a set of operations,or a contract, that a class can implement. It makes no sense for an interface to have anyrestricted access members, since restricted access members are normally associated with a classimplementation, and interfaces, by their definition, contain no implementation. Enumerations, onthe other hand, are normally used as a named collection of constants. Enumerations have no internalimplementation either, so it makes no sense for enumeration members to have any restrictedaccess. In fact, you get an error if you specify an access modifier, even public, on an interface memberor an enumeration member.As you can see, access for just about anything defaults to the strictest form of access that makessense for that entity. In other words, you have to do work to allow others access to classes or classmembers. The only exception is the access for a namespace, which is implicitly public and cannothave any access modifiers applied to it.InterfacesEven though I devote much of Chapter 5 to the topic of interfaces, it is worth introducing interfacesat this point for the purpose of discussion in the rest of this chapter. Generally speaking, an interfaceis a definition of a contract. Classes can choose to implement various interfaces, and by doingso, they guarantee to adhere to the rules of the contract. When a class inherits from an interface, it isrequired to implement the members of that interface. A class can implement as many interfaces asit wants by listing them in the base class list of the class definition.In general terms, an interface’s syntax closely resembles that of a class. However, each memberis implicitly public. In fact, you’ll get a compile-time error if you declare any interface member withany modifiers. Interfaces can only contain instance methods; therefore, you can’t include any staticmethods in the definition. Interfaces don’t include an implementation; therefore, they are semanticallyabstract in nature. If you’re familiar with C++, you know that you can create a similar sort ofconstruct by creating a class that contains all public, pure virtual methods that have no defaultimplementations.The members of an interface can only consist of members that ultimately boil down to methodsin the CLR. This includes methods, properties, events, and indexers. I cover indexers in the“Indexers” section, and I cover events in Chapter 10.■Note If you’re a stickler for terminology, the C# specification actually calls properties, events, indexers, operators,constructors, and destructors function members. It’s actually a misnomer to call them methods. Methodscontain executable code, so they’re also considered function members.The following code shows an example of an interface and a class that implements the interface:public interface IMusician//Note:A standard practice is that you preface interface names with a capital "I"{void PlayMusic();}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!