Designing Classes and Creating Instances
Identifying the objects in a system helps you design the class or blueprint for each object in the system. For example, window manufacturers often create a blueprint for every style of window they create. These blueprints define a range of colours and styles that can be selected when purchasing a window.
The blueprints are the basis for any number of windows with any number of combinations of colour and style. In terms of object-oriented design, each window is an object and the generic blueprint is a class. Each object created using the class is called an instance of a class.
More specifically, each object created from a class can have a specific state or value for each of the attributes it possesses, but will have the same attributes and operations.
The American Heritage Dictionary defines class to be "a group whose members have certain attributes in common."
Classes and Objects
Classes and objects are often used in the field of biology. For example, a marine biologist studying sea creatures is often asked to categorize sea creatures in a family, or class, of sea creatures. In terms of OOAD, each animal, such as a blue whale, in a family, such as whale, can be considered an object instance of the whale class.
DirectClothing, Inc. Case Study: Classes and Objects
A class is how you define an object. Classes are descriptive categories, templates, or blueprints. In the context of the DirectClothing, Inc. case study, a Shirt could be a class defining all shirts to have a shirt ID, size, color code, description, and price.
Objects are unique instances of classes. In the context of the DirectClothing, Inc. case study, a large blue polo shirt that costs R 29.99 with shirt ID 62467-B is an instance of the Shirt class, as is a small green shirt with the same price and shirt ID 66889-C, or a patterned shirt for R 39.99, ID 09988-A. You can also have two Shirt objects in memory with exactly the same attribute values.
Every class has a class that it originated from. In OOAD terminology, this parent class is referred to as superclass. For example, a whale might have originated from the superclass mammals.
Variables and Methods
In the Java programming language, attributes are represented using variables, and operations are represented using methods. Variables are the Java programming language mechanism for holding data. Methods are the Java programming language mechanism for performing an operation.
Variables also hold other data besides attribute data, such as values used solely inside of a method.
Modelling Classes
The first phase of the design stage consists of visually organizing or modelling a program and its classes. Each class in a design should be modelled so that it is enclosed in a box with the class name at the top, followed by a list of the attribute variables, including the range of possible values, and list of methods.
In the syntax, the ClassName is the name of the class and the attributeVariableName is the name of the variable for an attribute. Additionally, the range of values is an optional range of values that the attribute can contain, and the methodName is the name of a method.
For example, the graphic displayed depicts a modeled Shirt object. This modeling technique is vaguely based on a light version of the Unified Modeling Language (UML), which is a tool to aid in the modeling process.
Modeling classes is similar to modeling database structures. In fact, your objects can be stored in a database using the Java Database Connectivity™ (JDBC™) API. The JDBC API allows you to read and write records using structured query language (SQL) statements within your Java technology programs.
Format of Variables and Method Names
Variable and method names are written in a special short-hand so that a variable or method, representing any multiple-word attribute or operation, starts with a lowercase letter, and subsequent words are capitalized. For example, an operation, such as "calculate the total price," is written calcTotalPrice().
Further, a set of closed parentheses indicates a method.
The completed class diagram for the Shirt class is displayed in the graphic below: