A Programmer[ap]s Guide to Java Certification
Authors: Mughal K. Rasmussen R.
Published year: 2003
Pages: 28-30/284
Buy this book on amazon.com >>

1.7 Aggregation

When building new classes from existing classes using aggregation , a composite object is built from other constituent objects that are its parts .

Java supports aggregation of objects by reference, since objects cannot contain other objects explicitly. The fields can only contain values of primitive data types or references to other objects. Each object of the CharStack class has a field to store the reference to an array object that holds the characters . Each stack object also has a field of primitive data type int to store the index value that denotes the top of stack. This is reflected in the definition of the CharStack class, which contains an instance variable for each of these parts. In contrast to the constituent objects whose references are stored in fields, the values of primitive data types are stored in the fields of the composite object. The aggregation relationship is depicted by the UML diagram in Figure 1.7, showing that each object of the CharStack class will have one array object of char associated with it.

Figure 1.7. Class Diagram Depicting Aggregation

graphics/01fig07.gif


1.8 Tenets of Java

  • Code in Java must be encapsulated in classes.

  • There are two kinds of values in Java: object references and atomic values of primitive types.

  • References denote objects that are created from classes.

  • Objects can only be manipulated via references.

  • Objects in Java cannot contain other objects; they can only have references to other objects.

  • Deletion of objects is managed by the runtime system.


Review Questions

graphics/rq_icon.gif
1.1

Which statement is true about a method?

Select the one correct answer.

  1. A method is an implementation of an abstraction.

  2. A method is an attribute defining the property of a particular abstraction.

  3. A method is a category of objects.

  4. A method is an operation defining the behavior for a particular abstraction.

  5. A method is a blueprint for making operations.

1.2

Which statement is true about an object?

Select the one correct answer.

  1. An object is what classes are instantiated from.

  2. An object is an instance of a class.

  3. An object is a blueprint for creating concrete realization of abstractions.

  4. An object is a reference to an attribute.

  5. An object is a variable.

1.3

Which line contains a constructor in this class definition?

public class Counter {                                               // (1)
    int current, step;

    public Counter(int startValue, int stepValue) {                  // (2)
        set(startValue);
        setStepValue(stepValue);
    }

    public int get() { return current; }                             // (3)

    public void set(int value) { current = value; }                  // (4)

    public void setStepValue(int stepValue) { step = stepValue; }    // (5)
}

Select the one correct answer.

  1. Code marked with (1) is a constructor.

  2. Code marked with (2) is a constructor.

  3. Code marked with (3) is a constructor.

  4. Code marked with (4) is a constructor.

  5. Code marked with (5) is a constructor.

1.4

Given that Thing is a class, how many objects and how many reference variables are created by the following code?

Thing item, stuff;
item = new Thing();
Thing entity = new Thing();

Select the two correct answers.

  1. One object is created.

  2. Two objects are created.

  3. Three objects are created.

  4. One reference variable is created.

  5. Two reference variables are created.

  6. Three reference variables are created.

1.5

Which statement is true about an instance method?

Select the one correct answer.

  1. An instance member is also called a static member.

  2. An instance member is always a field.

  3. An instance member is never a method.

  4. An instance member belongs to an instance, not to the class as a whole.

  5. An instance member always represents an operation.

1.6

How do objects pass messages in Java?

Select the one correct answer.

  1. They pass messages by modifying each other's fields.

  2. They pass messages by modifying the static variables of each other's classes.

  3. They pass messages by calling each other's instance methods .

  4. They pass messages by calling static methods of each other's classes.

1.7

Given the following code, which statements are true?

class A {
    int value1;
}

class B extends A {
    int value2;
}

Select the two correct answers.

  1. Class A extends class B .

  2. Class B is the superclass of class A .

  3. Class A inherits from class B .

  4. Class B is a subclass of class A .

  5. Objects of class A have a field named value2 .

  6. Objects of class B have a field named value1 .

A Programmer[ap]s Guide to Java Certification
Authors: Mughal K. Rasmussen R.
Published year: 2003
Pages: 28-30/284
Buy this book on amazon.com >>