Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

More database stuff

Mastery aspects: Objects as records.

Recall that in Java the new keyword calls the constructor method of a class.

 

 

I know it isn't much fun to do the comments but better to do them now than go back over 2000 lines of dossier code and comment it all!

 

 

 

 

 

 

 

 

 

 

Again, we see the comments are shown in the create Object dialog.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The comments can be used to produce html documentation fo the class, saving a lot of effort later.

 

 

 

 

Standard level students shouldn't really worry that much about inheritance.

 

 

 

The toString() method is often useful for debugging code.

Here is the current state of the Biscuit Class code .

 

on this page: [ constructors | accessors | inherited ]

Constructors

The constructor is a special method used to create a new object instance. Just as with other methods, we can have more than one constructor, always provided that the parameters are different.

We have methods that change the properties of an instance, we can use these to make a constructor that creates a new biscuit object with properties we want to set:

/**
* Constructor sets the values using parameters
*
* @param the biscuit shape - coded, eg R = round
* @param the taste (or flavour) of the biscuit
* @param the price of the biscuit in dollars and cents
* @param the number of biscuits the price applies to
* @param true if this is my favourite biscuit
*/

public Biscuit( char shape, String taste, double price,
                 int unit, boolean myFavourite )
{
   setShape(shape);
   setTaste(taste);
   setPrice(price);
   setUnit(unit);
   setMyFavourite(myFavourite);
}

This method could be called from another Class to create a biscuit:

Biscuit custardCream = new Biscuit ('S', "Creamy", 1.50, 4, false);

Or directly from Bluej, if desired:

back to top

Accessor methods

The data members which carry the properties of each biscuit instance are private and therefore cannot be accessed outside of the Class.

This is generally considered good practice. It is possible that we will want to change the internal structure of the class at some future time but without affecting ("breaking") any code that might be using the Class.

Therefore we use accessor methods to return the properties of a particular instance:

/**
* This method returns the shape of the Biscuit
*
* @return the code representing shape of the biscuit
*/

public char getShape()
{
  
// recall that the this keyword can always be used with the
   // Class data members to avoid ambiguity.

   return this.shape;
}

It's quite acceptable to compress these methods if you like to save space, don't forget the comments though:

/**
* This method returns the taste of the Biscuit
*
* @return the description of this biscuit's taste
*/

public String getTaste() { return this.taste;}

Exercise

Complete the accessor methods.

back to top

Inherited methods

You might have noticed the BlueJ reference to "methods inherited from object" when right-clicking a created object instance on the workbench. In Java, all Classes inherit from the Object Class which has several methods of its own.

New Classes which are subclasses have access to all public (and protected ) methods of the superclass. We'll cover this further later on.

One of the more important methods of Object is toString() which gives a String representation of the Object. It is generally a good idea to replace this inherited method with a description of your particular object.

/**
* This method overrides toString() of Class Object
*
* @return a String represenation of the biscuit
*/

public String toString()
{
   return ( getShape() + " | "
          + getTaste() + " | "
          + getPrice() + " | "
          + getUnit() + " | "
          + isMyFavourite() );
}

Now, we have a data Class, next we'll move on to using it inside another Class - a Class to display Biscuits in a Panel .

If you are happier using the console for simple text-based programs you might simply want to alter the toString() method to make a prettier display on the console. Then youy won't need any of the code on the next page at all.

Related: [ Java home | previous: biscuit class | next: biscuit display ]

More about the Biscuit project.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009