4.4. Information Hiding/Accessibility

As we discussed earlier in this chapter, dot notation can potentially be used to access an object's field values, as in the following simple example:

Student x = new Student();

// Set the value of the name field of Student x.
					x.name = "Fred Schnurd";
					// Retrieve the value of x's name so as to print it.
					Console.WriteLine(x.name);

There is an obvious danger with giving all the data stored by a class public access. For example, if a student's GPA field was publicly accessible, an enterprising student could change his GPA simply by typing the following:

bob.gpa = 4.0;

In reality, therefore, objects often restrict access to some of their members (fields in particular). Such restriction is known as information ...

Get Beginning C# 2008 Objects: From Concept to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.