What defines the behavior of the objects that are created from the class?

1. The article describes that Plato�s theory encompassed not only physical objects like tables and rocks, but also abstract ideas like beauty and justice. In Java, a class can be defined as concrete, abstract, or final-concrete. A class has a name, which by convention starts with a capital letter. (For a complete description of valid identifiers, see the description of data members below.)

Concrete classes roughly correspond with Plato�s physical objects like tables and rocks, because they can be instantiated. For instance, given the class definition:

public class Table {}

it is possible to create an object of type Table, also known as an instance of the class Table, using the new keyword:

Table desk = new Table();

Classes are concrete by default. In other words, a class is assumed to be concrete unless it is designated otherwise.

Abstract classes roughly correspond with Plato�s abstract ideas of beauty and justice, because they cannot be instantiated. In other words, given the class definition:

public abstract class Beauty {}

It would be illegal to try to create an object of class Beauty, because Beauty is merely an abstraction. Thus the following code would cause a compiler error:

Beauty sunset = new Beauty();

Abstract classes are designated by the keyword abstract.

Abstract and concrete classes have the quality that they can be subclassed. A subclass of a given class, also known as a derived class, inherits all of the qualities of the parent class and specifies qualities that separate the subclass from the parent. Such a parent class is known as the superclass for the classes that derive from it. For example, a class Sedan is a possible subclass of a class Table0. Table0 would then be the superclass for the class Sedan. The ability of a class to encompass all of its subclasses and define qualities that are common to all of those subclasses is called polymorphism.

Polymorphism is implemented in the Java language through inheritance, which is designated by the Table3 keyword. For instance, the following class definition implements the relationship described above:

Table4

Final concrete classes are concrete classes that cannot be further subclassed. There are only very specific advanced situations when a concrete class must be designated as a final class. There are many situations, however, where a concrete class could be designated as a final class. For example, in the first laboratory assignment, the following class definition is provided:

Table5

The decision to designate this class as a final class is purely a design decision. In this case, there is no need to classify Table6 objects into further subclasses with differing qualities, so it is appropriate to designate the class Table6 as a final class. Instances of the class would have exactly the same behavior, however, if the class was concrete.

Final classes are designated by the Table8 keyword.

As an exercise, provide an empty class description that correctly describes each of the following types of objects or concepts. It is safe to invent classes like Table or Table0 as superclasses for classes of objects. This is an exercise in design; you will not have to implement any of these classes. The first few are provided to get you started thinking in polymorphic terms.

Class Class Description

Sedan Table1

Blue Table2

Basketball __________________________________________________

Screwdriver __________________________________________________

Piano __________________________________________________

Oxygen __________________________________________________

Happy __________________________________________________

2. Qualities of objects can be separated into two categories: form and behavior. The form of a class of objects encapsulates the types of qualities that separate objects of that class from objects of its superclass. In Java, this encapsulation is represented by a list of data members that represents the types of qualities common to objects of a given class. Each data member has an identifier, a type, an access modifier, and a default value. A data member is also known as an instance variable.

The identifier is the name of the data member, and can be any combination of letters, digits, and underscore characters that starts with a letter. Identifiers are case sensitive, which means that the identifiers Table3 and Table4 are considered to be different. Identifiers cannot contain any spaces, so the standard convention for Java is to begin an identifier with a lowercase letter and capitalize the first letter of each subsequent word. For instance, these are all legal identifiers that follow convention:

Table5

size

firstName

leftoverLunchFromLastMonday

The following are illegal identifiers:

Table6

The type is the class of the data member, and can be any public class or any one of the primitive types.

The access modifier for a data member designates the availability of the data member to objects of certain classes. The keyword Table7 designates that only objects of the class being defined have access to the contents of the data member. The keyword Table8 designates that only objects of the class being defined and objects of subclasses of the class being defined have access to the contents of the data member. The keyword Table9 designates that objects of any class have access to the contents of the data member. (Accessing a data member of an object is performed using the dot operator as described below.)

The default value for a data member can be any legal value or object of the type of the data member. If no default value is specified for a data member, its default value is set to 0 or new0 for a primitive type, or new1 for a reference type. The new1 keyword represents an object that has yet to be defined, i.e. an empty space to later store an object.

The following is a definition for a class containing three data members, one of which is an integer, one of which is a double-precision real number, and one of which is a new3 object:

new4

An instance new5 of class new6 could be created through the use of the new keyword:

new8

The data member new9, which is an integer value, could only be accessed within the class definition for new6, through the use of the symbol Table desk = new Table();1. For example, the following line of code is legal only if it appears within the class definition for new6:

Table desk = new Table();3

The data member Table desk = new Table();4, which is a real number value, could be accessed within the class definition for new6 or the class definition for a subclass of new6 through the use of the symbol Table desk = new Table();7. For example, the following line of code is legal only if it appears in the class definition for new6 or the class definition for a subclass of new6:

Beauty0

The data member Table5, which is an object of type new3, could be accessed within any class definition through the use of the symbol Beauty3. For example, the following line of code is legal in any class definition:

Beauty4

Each instance of a class can have different values for each of its data members. For example, one new6 can have a new9 of 1, a Table desk = new Table();4 of 3.16, and an instance of new3 corresponding with the color red, while another new6 can have a new9 of 2, a Table desk = new Table();4 of 6.55, and an instance of new3 corresponding with the color black.

A class is said to own its data members. Data members that are owned by a class can be accessed through the use of the Beauty3 keyword. The prefix Beauty4 can be eliminated for owned data members within a class definition, as the prefix is implied by context. For example, the following lines of code are equal within the definition for class new6 or the definition of a subclass of class new6, as a data member Table desk = new Table();4 would be owned by either class:

Beauty8

Data members can be designated static, final, or both static and final, if so desired. A data member is designated as static using the Beauty9 keyword. A data member is designated as final using the Table8 keyword. These designations are included between the access modifier and the type, like so:

Beauty sunset = new Beauty();1

A data member that is designated static is shared by all instances of a class. A static data member is also called a class variable.

A data member that is designated final may not have its value changed. The value of a final data member is also known as a constant.

A data member that is designated as static and final is both shared and constant. Most data members that are designated as final are also designated as static, by convention. Designating a final data member as static is more efficient than not designating a final data member as static, because only one shared copy of the constant value is maintained, rather than multiple copies of the constant value. Identifiers for data members that are designated as static and final follow a different convention than other data members. It is common practice to use an all capital letter identifier with words separated by underscore characters for a static final data member. For example, a default value for the new9 data member of new6 might be defined in the following fashion:

Beauty sunset = new Beauty();4

As a second exercise, fill in the following definition for a subclass of Beauty sunset = new Beauty();5 named Beauty sunset = new Beauty();6. The class is to have four data members:

Beauty sunset = new Beauty();7, an object of class new3 representing the color black. This data member has been defined for you.

Beauty sunset = new Beauty();9, a boolean variable representing whether or not the pen is ready to write, (i.e. the cap is off, or the button has been clicked that protrudes the ball point, &c.) This should be an instance variable accessible from any class definition. The default value should be new0.

abstract1, a double value representing the amount of ink left in the ink chamber of the pen, in units. This should be an instance variable accessible within the class definition for Beauty sunset = new Beauty();6 and the definitions of any of its subclasses. The default value should be 10.0 units.

Table5, an instance of class new3 representing the color of the ink in the ink chamber of the pen. This should be a constant class variable accessible only within the class definition for Beauty sunset = new Beauty();6. The default value should be Beauty sunset = new Beauty();7.

abstract7

3. The second major category for qualities of a class of objects is behavior. Behavior for a class of objects is defined as a list of messages that an instance of that class can respond to. In Java, these messages are called methods. Each method has an identifier, an access modifier, a return type, a parameter list, and a body. A method is also known as an instance method.

The identifier and access modifier are the same as for data members.

The return type is the type of value that the method will return when its computational task is completed. A method can return a value of any of the primitive types, or an object. A method that returns a reference type may return new1. The abstract9 keyword designates that a method does not return a value.

The parameter list is a comma-separated list of types and identifiers that the method accepts as input for the computational task it performs. A method can accept zero or more parameters in any combination of primitive or reference types.

The body is a mini-program that performs some computational task. The statements that make up the computational task for a method are enclosed in braces.

The most elementary kind of method included in a class description is a constructor, which is used in conjunction with the new keyword to create instances of the class. A constructor is the only kind of method that does not have an identifier. The default constructor for a class accepts no parameters and creates an instance of the class that has default values for all of its data members. Other constructors accept one or more parameters and create an instance of the class with non-default values. A Sedan1 statement is not required for a constructor, as it always returns an instance of the class. The following would be valid constructors for the new6 class defined above:

Sedan3

Two other fundamental kinds of methods included in a class description are inspectors and mutators. An inspector accepts no parameters and returns the value of one of the data members for the object. A mutator accepts one parameter and changes the value of one of the data members to the value passed as a parameter. The identifier convention for an inspector is Sedan4Member, where Member is the name of the data member whose value is to be returned. (The convention for an inspector of a boolean value is Sedan5Member.) The identifier convention for a mutator is Sedan6Member, where Member is the name of the data member whose value is to be changed. The following would be a valid inspector and mutator pair for the new9 data member of class new6:

Sedan9

There is a technique used in object-oriented programming called data hiding. Data hiding involves designating all data members for a class as private, and allowing access to data member values through a programming interface. A programming interface is simply a method or group of methods that allow inspection and/or mutation of a single data member. Using data hiding, the accessibility of a data member is determined by the availability of an inspector and/or mutator for the data member, and the access modifiers for those methods.

What is the behavior of an object defined by?

The behavior of an object is defined by its methods, which are the functions and subroutines defined within the object class. Without class methods, a class would simply be a structure.

What defines an object's behavior in Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes.

In what objects are defined by defining a class for them?

In object-oriented terminology, a class is a template for defining objects. It specifies the names and types of variables that can exist in an object, as well as "methods"--procedures for operating on those variables. A class can be thought of as a "type", with the objects being a "variable" of that type.