Encapsulation
Our discussion of classes and objects is integral to us using object-oriented programming. Object-oriented programming stands on four pillars: abstraction, encapsulation, inheritance, and polymorphism.
Encapsulation
Encapsulation is the bundling of related data and behaviors that operate on that data, usually with restricted access to internal, non-public data and behaviors. In object-oriented programming, classes and objects allow us to encapsulate, or isolate, data and behavior to only the parts of our program to which they are relevant. Restricting access allows us to expose only that data and behavior that we want others to be able to use.
Let’s take a look at this by developing a new class called Student
.
Student
Class
Fields
We previously defined a field as a variable, or piece of data, that
belongs to a class. For our Student
class, letâs think about the
data that is typically associated with a student (in the sense of a high
school or college student). There are a lot of possibilities, but here
are the most important:
- Name
- Student ID
- Number of credits
- GPA
In order to declare these fields within our class, weâll need to determine the best data type for each. A field may be of any primitive or object type. In this case, the following types will work best:
- Name:
string
- Student ID:
int
- Number of credits:
int
- GPA:
double
Letâs put these inside of a class. While they may be declared anywhere within a class, fields should always be declared at the top of the class. When weâre ready to add methods, weâll add them below the fields.
|
|
Like variables within a method, fields may be initialized when they are
declared. For example, we could provide default values for
numberOfCredits
and gpa
(default values for name
and
studentId
donât make sense since they should be different for each
student).
int numberOfCredits = 0;
double gpa = 0.0;
Class members default to private
if no access modifier is provided. This means
that our Student
fields are inaccessible to code outside of the Student
class. As a
rule-of-thumb, fields should always be private unless you have a very,
very, very good reason to not make them so. As we mention on the previous page, it is
best practice to think carefully about what access to give fields and methods. So, letâs
explicitly declare our fields to be private.
|
|
Getters and Setters
In order to provide access to private fields, getter and setter methods are used. Getters and setters do what you might guess: get and set a given field. If we make the getter and/or setter method for a given property public, then others will be able to access or modify the field in that way.
Getter setter methods are also often called accessors.
Here is a getter/setter pair for name
(you can imagine how the
others would be written).
|
|
Here, within get
and set
, name
refers to the private field
that stores the value of the property. In set
, the special variable
value
will contain the value that the user is trying to set within the property.
We can then get or set the value of Name
anywhere else (since it’s public)
using dot-notation:
|
|
When you use properties in this way, the get/set methods are called implicitly when assigning or reading the property.
An astute question to ask at this point would be, âWhy make the fields private if youâre just going to allow people to get and set them anyway!?â Great question. There are lots of reasons to use getters and setters to control access. Here are just a few:
- Getters and setters allow you to implement behavior that happens every time a
field is accessed (get) or changed (set). For example, you may want track the
number of times a change is made to a field. With a private field and setter
method, this can be done simply by incrementing a counter variable (e.g.
i++
.) With a publicly available field, the steps to track its changes would be much more diffuse, if not error-prone. - You can perform validation within a setter. For example, we might want to ensure that a studentâs name contains only certain characters, or that their student ID is positive.
- You can use different access modifiers on getters and setters for the same field, based on desired usage. For example, you might want to allow anyone to be able to read the value of a field, but only classes within the same assembly to modify it. You could do this with a public getter and an internal setter, but not as a field without getters and setters, which could only be public to everyone or internal to everyone.
One of the four fields in our Student
class is a prime candidate for
the scenario described in item 3. Which one do you think it is?
To set access levels on accessors so that they are different than the access
level of the property, use an access modifier next to get
or set
. Here’s
how we would make Name
readable by everyone, but modifiable only by code within
the class’s assembly. Note that the get accessor does not have an access modifier in
front of it and therefore it will have the same public access as the property Name
.
|
|
As an example of setter validation, letâs take a short detour to look at a
Temperature
class. A valid temperature can only be so low (âabsolute
zeroâ), so we wouldnât want to allow somebody to set an invalid value.
In set
, we throw an exception if an invalid value is provided (we’ll
cover exceptions
in detail later, but for now note that they are ways of
signaling errors).
|
|
Properties
A property in C# is a characteristic that users can set.
Most often, properties will correspond directly to a private backing field,
but they don’t have to. Letâs look at an example of a
property that doesnât directly correspond to a field. If we wanted to
add a Celsius
property to the Temperature
class above, we might
do it as follows:
|
|
Since thereâs a link between Fahrenheit
and Celsius
, we want to make
sure that when one is updated, so is the other. In this case, we only
store one field value (fahrenheit
) and make the appropriate
calculation when getting or setting the Celsius
property.
Auto-Implemented Properties
If a field has both a public getter and setter, and no additional logic is needed, we can use the shorthand:
public string Name { get; set; }
This is referred to as an auto-implemented property. When a property is auto-implemented, the compiler creates a private field that can only be accessed through the property’s get and set accessors.
Note that in this example, the private field is name
(lowercase)
while the property is Name
. Since C# identifiers are case-sensitive, these are
two distinct members. name
is referred to as a backing field, and it stores
the value of the property.
At this point you may be asking yourself, why would I use an auto-implemented property instead of just making a field public? It comes down to properties being more flexible to change in the future, such as changing the accessibility of only the getter or setter, and more capable when used in your user interface through data binding, which we will explore more in later chapters.
If you were to try to use the same identifier for both the backing field and the property, you’ll see a StackOverflowException due to infinite recursion – i.e., the property would infinitely call itself!
Using properties, getters/setters, and fields, we can encapsulate the information we need in our student class.
Check Your Understanding
What is a method that is used to give a private field a value?
- getter
- method
- property
- setter