Variables and Methods

 

Data located within an object are referred to as variables or attributes. Methods are functions within an object that manipulate the object's variables or perform an operation relevant to the object. In the diagrams below, variables are shown as rectangles and methods as ovals.

Methods provide the primary interface between an object and the outside world (i.e., other objects). They allow a programmer to hide both the variables associated with an object and the implementation of function supported by the object.

Although variables are normally hidden from the outside world and accessed through the methods the object provides for that purpose, this is not always the case. In Java, variables may also be declared public and, thereby, accessed directly by another object. In the figure, below, the class includes both public and private variables as well as public methods. The reader should note, however, that using public variables is not generally considered good o-o programming practice.

Variables/Attributes. Shows an object that includes public methods and private variables, the preferred o-o structure. Methods. Shows an object that includes public and private variables as well as public and private methods.