Java Lesson 10: Encapsulation

Advertisements

Hello everybody,

It’s Michael, and I’m back with another series of Java lessons (but there will be plenty more Pythons lessons to come). Today’s Java lesson will be on encapsulation.

Encapsulation is a process in Java that wraps (or encapsulates) code and data together in a single unit. The whole point of encapsulation is to ensure that sensitive program data (whatever that might entail) is hidden from users.

There are two things you need to do to in order to encapsulate your data, which include:

  • declaring class variables as private, which means that they can only be accessed in the same class
  • provide getter and setter methods to access and modify the value of a private variable.

Getter methods return the value of a private variable while setter methods set the value of a private variable. Here’s an example of a program with getter and setter methods:

public class ProgramDemo
{
private String model;
private int year;

public String getModel ()
{
return model;
}

public void setModel (String newModel)
{
this.model = newModel;
}

public int getYear ()
{
return year;
}

public void setYear (int newYear)
{
this.year = newYear;
}
}

In this program, there are two private variables relating to cars-model (referring to what model the car is) and year (referring to the car’s release year). Both variables have getter and setter methods. For each of the getter methods, the syntax would be like this:

  • public [variable type] get[variable name with first letter capitalized] ()

The body of the getter method would consist of a return statement followed by the variable’s name (such as return model). The setter methods are structured differently; here’s the syntax:

  • public void set[variable name with first letter capitalized] (variable type followed by variable name)

Regardless of variable type, all setter methods are of type void, meaning that they have no return statement. Also, when I mentioned that the parameters for setter methods are the variable type followed by the variable name, the variable name would have its first letter capitalized and it would have the word new in front of the name (like I did with newYear and newModel).

The body of setter methods consists of a single line of code (just as with getter methods). That single line of code would appear as this.[variable name] = new[variable name with first letter capitalized]. The word this refers to the current object and will always appear in the body of setter methods.

Now let’s see our setter and getter methods at work, using a separate class called Car:

public class Car
{
public static void main (String [] args)
{
ProgramDemo myCar = new ProgramDemo ();
myCar.setModel(“Honda Civic”);
myCar.setYear(2007);
System.out.println(myCar.getModel());
System.out.println(myCar.getYear());
}
}

Using both of my setter methods, I set the model to Honda Civic and the year to 2007. I then use both of my getter methods-along with System.out.println-to display the model name and year of the car as the output (which is shown below)

Honda Civic
2007

  • The best part about this program is that I didn’t need to use the extends statement to access the getter and setter methods in ProgramDemo. Rather, I simply created the variable myCar as an object of the ProgramDemo class and I was able to access the getter and setter methods for both variables.

Thanks for reading,

Michael