What is encapsulation?
Example - The Time Class
Yousuf has created the Time class shown below:
Public Class Time
{
// member variables - defined as private so that they can not be directly accessed
// outside of the class.
private int hours;
private int minutes;
private int seconds;
}
Public Class Time
{
// member variables - defined as private so that they can not be directly accessed
// outside of the class.
private int hours;
private int minutes;
private int seconds;
}
Because Yousuf has correctly set the member variables as private. He must create accessor methods.
Accessor Methods
Accessor methods are often called "get/set" methods because they get or set a member variable. It is conventional to name them get ( or set) followed by the variable name.