Each of the devices enumerated above supplies the user with an interface—

switches, buttons, remotecontrols, whatever--

for operation. Technical details are tucked away and hidden from users. Eachencapsulated item functions perfectly well as a “black box.” Certainly, Joe User need not understandhow

his Radio Shack gadget is constructed in order to operate it correctly. A user-friendly interface andperhaps an instruction manual will suffice.

Encapsulation has a similar (though somewhat expanded) meaning when applied to softwaredevelopment and object oriented programming:

The ability to provide users with a well-defined interface to a set of functions in a way which hidestheir internalworkings. In object-oriented programming, the technique of keeping together datastructures and the methods (procedures) which act on them.

–

The Online Dictionary of Computing

Java provides encapsulation, as defined above, via classes and objects. Aswe have already seen,classes bundle data and methods into a single unit.Classes encapsulate.

Example:

Consider the Square class that is defined below. Square contains a few data fields, three constructorsand several methods. Most of the code should be fairly easy to understand.

public class Square

{

// notice that the private (public) keyword must be repeated for each new declaration

private int dimension;

private char character; // character to be used for drawing

//constructors

public Square() //default constructor

{

dimension= 0;

character = ' ';

}

public Square(int x, char ch)

//two argument constructor

{

dimension = x;

character = ch;

}

//accessor methods--

getter methods

public int getDimension()

{

return dimension;

}

public char getCharacter()

{

return character;

}

//Mutator Methods--

setter methods

public void setDimension(int x)

{

dimension = x;

}

public void setCharacter(char ch)

{

character = ch;

}

public int area() // calculates the area of a square

{

return dimension*dimension;

}

public int perimeter() // calculates the perimeter of a square

{

return 4*dimension;

}

public void draw(int x, int y) // draws a square at position (x,y)

{

// moves down y lines, indents x spaces and

// draws a Square with the designated character

// move the cursor down y lines

for

( int i = 1; i <= y; i++)

System.out.println();

for (int len = 1; len<= dimension; len++)

{

// indent x spaces

for (int i = 1; i <= x; i++)

System.out.print(' ');

//draw one "line" of the Square using character

for (int j = 1; j<= dimension; j++)

System.out.print(character);

System.out.println();

}

}

public static void main(String args[])

{

Square s;

s= new Square(6,'*'); // dimension is 6 X 6

s.draw(8,15);

}

}

Please notice the following:



the keywordspublicandprivate.



Class Squaremustbe saved in a file called Square.java.

The Square class provides a simple example of encapsulation–

data and methods, attributes andfunctionality, are combined into a single unit, a single class. Furthermore,

all data are accessed notdirectly but through the class methods, i.e. via an interface.

The user of Square–

the client--

need not knowhow

the class is implemented–

only how touse

theclass. Variable names are of no concern to the client. If the client wishes to know the length of a Squareobject, the interface provides an accessor method. If the client wants to change the size of a Squareobject, the client simply uses the mutator method available through the interface. That the length of thesides of a square is held in a variable calleddimensionis irrelevant to the client.

Further, if theimplementation of the class is modified, programs utilizing the Square class will not be affected providedthe interface remains unchanged. Like a TV or a

camera, the inner workings of the Square class areencapsulated and hidden from the client. Public methods provide the interface just as the remote controlunit provides the interface for a TV viewer.

Another term that is often associated with encapsulation isinformation hiding. Many authors regardencapsulation and information hiding as synonyms. However OO purists might define encapsulation asthelanguage feature

allowing the bundling of data and methods into one unit and information hiding asthe

design principle

that restricts clients from the inner workings of a class. With this distinction, programscan have encapsulation without information hiding. Regardless of how you define your terms, classesshould be designed with a well-defined interface in which implementation decisions and details arehidden from the client.

Square Objects and References

You will notice that the Square class has a main() method. main() is included for testing as well as forillustrative purposes. Every class need not contain a main() method.

Take a look at the main() method. Notice that a Square object is created orinstantiated

in two steps:

1.

Square r;

2.

r = new Square(6,’*’);

Variable r isNOT

a Square object but areference

to a Square object. This is in contrast to C++. Thedeclaration in Line 1 does not cause a Square object to be constructed. No constructor is called. Thereference r can hold theaddress

of a Square object only once a Square object is created. Initially r hasthe valuenull.

Line 2 constructs, builds, instantiates, or creates a Square object and also assigns the address of thisnewly minted Square to reference r. Notice the use of thenewoperator as well as a call to the (two-argument) constructor.

Line 1

Line 2

It is common practice to speak of a reference variable as an object. Often, one says that “r is a Squareobject” or “r is a Square.” The fact is, however, that r is areference

to a Square object, but r is not aSquare object itself. This distinction between a reference and an object is an important one.

Using the Square Class

Once we have designed the Square class, it is a snap to use it in a program. The following programallows a person to continually invoke the methods of the Square class, possibly to draw any number ofsquares on the screen.

It is certainly tempting to place the program application into the main method of the Square class. Theprogram would certainly work and do the job. Nonetheless, in the object-oriented paradigm,objectsperform tasks by sending messages to other objects.

Drawing a Square on the screen, means sending amessage to a Square object. (“Square, draw thyself!”). An object of the following class, aptly namedPicasso, communicates with a Square object,square, by sending messages to square--

messages like“draw

yourself,” “get your area,” “change your width” etc.

public class Picasso

{

private Square square; // needs a Square object with which to communicate

public Picasso()

{

// default constructor

// creates a Square object from the user’s specifications

System.out.print("Size: ");

int s = MyInput.readInt();

System.out.print("Drawing character: ");

char c = MyInput.readChar();

square= new Square(s,c); // here is a Square object.

}

private int menu()

{

// a helper function to present choices to the user

int choice;

{

System.out.println("1: set side");

System.out.println("2: set drawing character");

System.out.println("3: draw");

System.out.println("4: get area");

System.out.println("5: get perimeter");

System.out.println("6: exit");

do // loop until user enters a number in the range 1-6

{

System.out.print("Choice: ");

choice = MyInput.readInt();

}while (choice <1 || choice > 6);

return choice;

}

}

public void drawPlus()

{

// Based on the user’s choice, this method

// sends messages to the Square object, square. The messages

// "ask" square to draw itself, get its area, perimeter etc.

int choice;

do

{

choice = menu();

switch (choice)

{

case 1:System.out.println("Enter side:");

// send square a message

square.setDimension(MyInput.readInt());

break;

case 2: System.out.println("Enter character:");

square.setCharacter(MyInput.readChar());

break;

case 3: System.out.println("Enter x and y:");

square.draw(MyInput.readInt(), MyInput.readInt());

break;

case 4: System.out.println("Area is "+ square.area());

break;

case 5: System.out.println("Perimeter is "+ square.perimeter() );

break;

case 6: System.out.println("Bye");

}

}while (choice != 6);

}

public static void main(String args[])

{

Picasso artist = new Picasso(); //a Picasso object

artist.drawPlus(); // triggers the action

}

}//endPicasso. This class should be saved in Picasso.java.

In addition to main(), the Picasso class has two public methods:

a constructor which creates a Square object (square) and

a drawPlus() method which sends messages to the Square object.

These messages tell square to draw itself, give its area, its perimeter etc. Picasso also has a private“helper” method–

menu(). (Rather than writing this separate helper method, the code for menu() couldhave been incorporated into the drawPlus() method or a menu object might have been created from aMenu class).

In short, two objects, artist (a Picasso object) and square (a Square object), communicate with each otherto accomplish a task.

The main method of our application is simple and uncomplicated.



The main method creates an instance of the Picasso class (artist) and



starts the action by calling the drawPlus() method that sends a message to artist