Java - Encapsulation

December 2016




The concept of accessibility


The concept of accessibility (usually called encapsulation) defines the ability of the designer of a class to restrict access to certain data, or more generally elements (methods, classes, ...). Some classes are (freely or commercially) released in order to be reused by some programmers. However, it is generally desirable that users, make use of the class in the way it was intended to be. Thus, it is possible to restrict access to certain elements by associating a label.

Protect data members


Encapsulation is the process where you define labels for data members and methods to specify whether they are accessible from other classes or not ...
In this way, data members labeled as "private" can not be directly manipulated by the methods of other classes.
  • Member functions for accessing the data members are called accessors, sometimes getter.
  • Member functions to modify the data members are called switches, sometimes setter.
  • All methods allowing the user to manipulate a class are called "interfaces".

Access levels


Java defines four levels of access for instance variables (data members) and methods:
  • public: a public element is accessible from anywhere and without any restriction. Some classes must be declared public, to run the application.
  • protected: the "protected" access is available only to classes of a package and its subclasses
  • private: private element is accessible only within the class in which it is declared. These items can be handled only with specific methods called accessor and mutator.
  • Friendly: an item is friendly by default (this name is not official and is borrowed from C ++) and is available only to classes of a package and its subclasses.

The concept of accessor


An accessor is a method to retrieve the contents of a protected data member. For an accessor to perform its task:
  • must have the same return type as the return variable.
  • arguments are not compulsory.

The syntax of an accessor in its simplest form:

public class MaClasse{
	public static void main(String[] argv){
		new MaSecondeClasse();

	}
}

class MaSecondeClasse{
		private TypeDeMaVariable maVariable;

		public TypeDeMaVariable getMaVariable(){
			return maVariable;

		}
}

In the above example, the minimum accessor for the "age" data member could be the following:
class Toto{
	private int age;

	public int getAge(){
		return age;

	}
}

The concept of mutator


A mutator is a method to change the contents of a protected data member. For an mutator to perform its task:
  • It should have as parameter the value to assign to the data member. The parameter must be of the type as the data member.
  • There's no need to return a value.

The syntax of a mutator in its simplest form:

class MaClasse{
	private	TypeDeMaVariable maVariable;

	public	void setMaVariable(TypeDeMaVariable maValeur){
		maVariable = maValeur;

	}
}


In the above example, the mutator could be the following:
class Toto{
	private int age;

	public void setAge(int nouveauAge){
		age = nouveauAge;

	}
}


The main advantage of such a mechanism is the control of the validity of the data members it provides. Indeed, it is possible (and recommended) to test the value that is assigned to a data member:

class Toto{
	private int age;

	public int setAge(int nouveauAge){
	if (nouveauAge < 200) {
		age = nouveauAge;

		return 1;

	}
	else return 0;

	}
}



Original document published on CommentcaMarche.net.

Related :

This document entitled « Java - Encapsulation » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.