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.
public class Student {
String name;
int studentId;
int numberOfCredits;
double gpa;
}
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;
Fields are also referred to as instance variables, since they belong to an instance of a class.
Getters and Setters
As declared, our four fields are package-private, which means that they can be read or changed by any code within the same package. As a rule-of-thumb, fields should always be private unless you have a very, very, very good reason to not make them so. So, letâs make our fields private.
public class Student {
private String name;
private int studentId;
private int numberOfCredits = 0;
private double gpa = 0.0;
}
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.
Here is a getter/setter pair for name
(you can imagine how the others would be written).
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
Prefixing a parameter that is intended to set an instance variable with a
is a relatively common convention, and one that weâll adopt to
avoid shadowing and having to use this
in our setters. You can think of the a
as denoting the âargumentâ version of the variable.
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:
- Sometimes youâll want to implement behavior that happens every time a field is accessed (get) or changed (set). Even if you canât think of such a reason when writing your class, you might later have the need to add such behavior. If you donât use getters and setters, youâll have to do a lot more refactoring if you ever decide to add such behaviors.
- 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 package to modify it. You could do this with a public getter and a package-private setter, but not as a field without getters and setters, which could only be public to everyone or package-private to everyone.
As an example of reason 2, 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 setFahrenheit
we print out if an invalid value is provided.
public class Temperature {
private double fahrenheit;
public double getFahrenheit() {
return fahrenheit;
}
public void setFahrenheit(double aFahrenheit) {
double absoluteZeroFahrenheit = -459.67;
if (aFahrenheit < absoluteZeroFahrenheit) {
System.out.println("Value is below absolute zero");
}
fahrenheit = aFahrenheit;
}
}
When writing getters and setters, the convention for a field named field
is to name them getField
and setField
. This is more than just a convention, as some libraries you use will expect names to be of this format, and wonât work as desired if you donât follow the convention.
Additionally, itâs a standard convention to use is
instead of get
for boolean fields. So a boolean field oldEnoughToVote
would have the âgetterâ method isOldEnoughToVote
. The setter should still be named setOldEnoughToVote
.
Properties
A property in Java is a characteristic that users can set. Our Student
class had properties name
, studentId
, numberOfCredits
, and gpa
, while our Temperature
class had only one property, fahrenheit
.
Most often, properties will be fields that have public setters, though they need not have a corresponding field. 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:
public double getCelsius() {
return (fahrenheit - 32) * 5.0 / 9.0;
}
public void setCelsius(double celsius) {
double fahrenheit = celsius * 9.0 / 5.0 + 32;
setFahrenheit(fahrenheit);
}
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.
There are slight variations among Java developers when it comes to colloquial usage of the term property. People will sometimes define the term in a slightly more specific or narrow way, to mean a private field with public getters and setters.
Our definition here relies on the more general definition given by Oracle.
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