Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Study guide: Object-oriented language basics, Part 3

Brush up on Java terms, learn tips and cautions, and review homework assignments

  • Print
  • Feedback

Glossary of terms

composition
The act of composing an object from other objects, also known aggregation.


has a relationship
A relationship between two objects, where one object has (contains) another object. The presence of a reference field in the containing object makes that relationship possible. That field contains a reference to the contained object.


Tips and cautions

These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.

Tips

  • To introduce composition into a class, you need to only declare a field of some reference type inside that class. However, because you might want to make that reference field private (for information-hiding reasons), you will want to introduce a constructor with a parameter of the same reference type, and place code within the constructor that assigns the parameter's value to the reference field. For example: class Vehicle { private Engine e; Vehicle (Engine e) { this.e = e; } }.


Cautions

  • During the class design phase of solving a software problem, think carefully about the relationships between the problem's various entities. Make sure you know which relationships are has a (which indicates composition) as opposed to is a (which indicates inheritance). If you confuse those relationships, you run the risk of creating source code difficult to understand and maintain.


Answers to last month's homework

Last month, I presented you with a single exercise and two questions. Here are those questions and my answers in red:

  • Create a program for manipulating complex numbers -- numbers that have the form realPart + imaginaryPart * i, where i represents the square root of -1. The program should include the following:

    • A Complex class that represents the real and imaginary parts in private floating-point variables
    • A constructor that initializes the private floating-point variables
    • public get methods that return the real and imaginary parts
    • A public add(Complex c) method that separately adds the real parts of the current and c argument Complex objects together, and the imaginary parts of both objects together
    • A public subtract(Complex c) method that separately subtracts the real part of the c-referenced Complex object from the current Complex object, and the imaginary part of the c-referenced Complex object from the current Complex object
    • A public print() method that prints the contents of the current Complex object using the format (a, b) -- where a represents the real part and b represents the imaginary part


    The following listing provides source code to a UseComplex application, with a Complex class and a UseComplex class that creates and manipulates Complex objects.

    UseComplex.java

    // UseComplex.java
    class UseComplex
    {
       public static void main (String [] args)
       {
          Complex c1 = new Complex (2.0, 5.0); // 2.0 + 5.0i
          Complex c2 = new Complex (-3.1, -6.3); // -3.1 - 6.3i
          c1.add (c2); // c1 is now -1.1 - 1.3i
          c1.print ();
       }
    }
    class Complex
    {
       private double re, im;
       Complex (double real, double imag)
       {
          re = real;
          im = imag;
       }
       public double getRe ()
       {
          return re;
       }
       public double getIm ()
       {
          return im;
       }
       public void add (Complex c)
       {
          re += c.getRe ();
          im += c.getIm ();
       }
       public void subtract (Complex c)
       {
          re -= c.getRe ();
          im -= c.getIm ();
       }
       public void print ()
       {
          System.out.println ("(" + re + "," + im + ")");
       }
    }
    
    
  • Why are enumerated types important?

    An enumerated type is important because its restricted set of values prevents irrational code that can lead to bugs. Such irrational code arises from the use of integers (instead of enumerated types) to distinguish among objects of the same type.

  • When is a singleton not a singleton? (Hint: Explore JavaWorld's Website for an article on singletons.)

    Read "When Is a Singleton Not a Singleton?" by Joshua Fox (JavaWorld, January 2001).



Homework

Answer the following question and work on the following exercise:

  • In what way does Listing 1's CarDemo application already use composition?
  • Rewrite CarDemo to use composition.


  • Print
  • Feedback

Resources