< Zurück | Inhalt | Weiter >

3.4.1 Encapsulation

Encapsulation is the grouping of data and algorithms together into units, and it’s also about hiding implementation details that are not important to the users of the unit. The basic unit of encapsulation is the class. All Java code exists in classes. A class is declared with the class keyword (Example 3.24).



image

Example 3.24 A sample Java class declaration that doesn’t actually do anything useful

public class Sample

{

private int id;


public void method()

{

System.out.println(id);

}

}


image