<< Chapter < Page
  Xna game studio     Page 4 / 12
Chapter >> Page >

The C# programmer has control over the code that is executed in response to the event. However, the C# programmer has no control over the fact that a Button object will respond to such an event. The fact that a Button will respond to certain event types is an inherent part of the type specification for the Button class and can only be modified by modifying the source code for the Button class.

Encapsulation

If abstraction is the design or specification of a new type, then encapsulation is its definition and implementation.

A programmer defines the data representation and the behavior of an abstract data type into a class, thereby defining its implementation and its interface.That data representation and behavior is then encapsulated in objects that are instantiated from the class.

Expose the interface and hide the implementation

According to good object-oriented programming practice, an encapsulated design usually exposes the interface and hides the implementation. This isaccomplished in different ways with different languages.

Just as most of us don't usually need to care about how the steering mechanism of a car is implemented, a user of a class should not need to careabout the details of implementation for that class.

The user of the class (the using programmer) should only need to care that it works as advertised. Of course this assumes that the user of the class hasaccess to good documentation describing the interface and the behavior of objects instantiated from the class.

Should be able to change the implementation later

For a properly designed class, the class designer should be able to come back later and change the implementation, perhaps changing the type of data structureused to store data in the object, and the using programs should not be affected by the change.

Class member access control

Object-oriented programming languages usually provide the ability to control access to the members of a class. For example, C#, C++ and Java all use thekeywords public , private , and protected to control access to the individual members of a class. In addition, Java adds a fourth level of access control, which is called package-private . C# adds two levels of access control that are not included in Java or C++ (see the next section).

Five levels of access control

According to the C# specifications , "Each member of a class has an associated accessibility, which controlsthe regions of program text that are able to access the member." There are fivelevels of access control in C#:

  • public - Access not limited
  • protected - Access limited to this class or classes derived from this class
  • internal - Access limited to this program
  • protected internal - Access limited to this program or classes derived from this class
  • private - Access limited to this class

A different interpretation

Another online source provides a different interpretation for two of the access levels:

  • The internal modifier declares that a member is known throughout all files in an assembly, but unknown outside that assembly.
  • The protected internal access level can be given only to class members.
  • A member declared with protected internal access is accessible within its own assembly or to derived types.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask