OOPortal OOPortal

User Defined Types  «Prev 
x: Floating Point
y: Floating Point

Java Implementation

In Java, the program corresponding to this diagram would be written like this:
class Point {
  double x;
  double y;

Java Classes

In Java, all code is grouped into classes. A class is thus a code container. The definition of the class starts with an access modifier (protected in this case), which specifies which classes have access to it. This is followed by the keyword class and the name of the class (Point). Every class definition is enclosed within brackets {}.
It has variables (x,y) and no methods as of yet. The main method is a special method since it is the entry point of program execution. In other words, when the class Point is run by the Java Runtime Environment, it will start by executing the main method. Note that not every Java class should have a main method.

Class Diagrams

Class diagrams are used to describe the structure of the system. Classes are abstractions that specify the common structure and behavior of a set of objects. Objects are instances of classes that are created, modified, and destroyed during the execution of the system. An object has state that includes the values of its attributes and its links with other objects. Class diagrams describe the system in terms of objects, classes, attributes, operations, and their associations. For example, Figure 5-3 is a class diagram describing the elements of all the watches of the SimpleWatch class. These watch objects all have an association to an object of the PushButton class, an object of the Display class, an object of the Time class, and an object of the Battery class. The numbers on the ends of associations denote the number of links each SimpleWatch object can have with an object of a given class. For example, a SimpleWatch has exactly two PushButtons, one Display, two Batteries, and one Time. Similarly, all PushButton, Display, Time, and Battery objects are associated with exactly one SimpleWatch object.

A UML class diagram describing the elements of a simple watch.
Figure 5-3: A UML class diagram describing the elements of a simple watch.