C SC 160 Chapter 7: Defining Your Own Classes (advanced)
major resource: An Introduction to Object-Oriented Programming with Java, fourth
edition,
Wu, McGraw Hill, 2006
[ previous
| schedule
| next ]
Packages
Packages provide a way to organize programs and libraries that contain multiple classes
- The package name reflects name of the directory (folder) that contains its classes and subpackages.
- Example: Package java.util is represented by folder java/util, and the classes
in this package such as Random, EventObject, etc., are represented by
files Random.class, EventObject.class, etc. in that folder
Javadoc comments
- javadoc is utility tool to produce HTML documentation directly from Java source code
- the online Java API documentation was produced by javadoc
- javadoc is invoked in jGRASP from the CSD (source code) window by either
- clicking on the open-book icon on the CSD toolbar, or
- selecting "Generate Documentation" from the CSD "File" menu
- documentation is produced from combination of Java syntax and specially-formatted comments
- javadoc will search comments that start with "/**" for special "tags" such as
- @author (followed by author's name)
- @param (followed by parameter name and description)
- @return (followed by description of return value)
- Here's an example, for the fromDollar() method of a CurrencyConverter class
/**
* Converts a given amount in dollars into
* an equivalent amount in a foreign currency.
*
* @param dollar the amount in dollars to be converted
*
* @return amount in foreign currency
*/
public double fromDollar(double dollar) {
return (dollar * exchangeRate) ;
}
The above comments, when processed by javadoc in the context of a fully-commented CurrencyConverter
class, will produce two portions of the documentation.
- the method's listing in the Method Summary
- the method's listing in the Method Details
Study closely the correspondence between the code and the resulting documentation.
Nice!
Overloading Constructors
A constructor is a special method which creates objects. We covered it
in Chapter 4. Recall that its major function is to initialize
any instance variables.
Here is example constructor from a CurrencyConverter class
that has one instance variable, double exchangeRate;
public CurrencyConverter( double rate ) {
exchangeRate = rate;
}
Programmers often provide multiple constructors having default values for different
combinations of instance variables. CurrencyConverter has only one instance
variable, so consider one additional constructor that gives it a default value
of 1.0.
public CurrencyConverter( ) {
this(1.0);
}
This constructor has no parameters, and works by invoking the other constructor with an argument
value of 1.0. It is not unusual to define one constructor to do all the work and define the others to
call it with the desired configuration of supplied and default values.
Here's another example of the technique, which shows 4 constructors
import java.awt.Color;
public class Square {
private double height;
private Color color;
public final double DEFAULT_HEIGHT = 1.0;
public final Color DEFAULT_COLOR = Color.BLACK;
public Square(double ht, Color col) {
height = ht;
color = col;
}
public Square(double ht) {
this(ht, DEFAULT_COLOR);
}
public Square(Color col) {
this(DEFAULT_HEIGHT, col);
}
public Square( ) {
this(DEFAULT_HEIGHT, DEFAULT_COLOR);
}
/********* remainder of class omitted *********/
}
This is called overloading. The compiler can distinguish between different versions of the constructor based on the different
parameter types and counts (int versus String parameter, 1 versus 2 parameters, etc)
The examples above show a new use for the reserved word this. It is used to explicitly
invoke one constructor from inside another constructor in the same class. Using this technique,
one can define a multiple-parameter constructor to do the construction work then invoke it from lesser-or-no
parameter constructors that pass on default initial values.
Overloading other methods
Overloading is not limited to constructors. Any method can be overloaded, so long as each version of
the method has a distinguishable parameter list.
There is a related concept called overriding, that we have not considered. Overriding
occurs when you write a method that has the same name and parameters as one of your inherited
methods. The new version overrides the inherited one.
Objects as parameters and arguments
Methods can have parameters that are of object, not primitive, types.
- When you invoke such a method,
you have to provide an object reference in the corresponding argument position.
- The pass by value rule still applies, but in this case the value is the reference.
In other words, the object's address.
- Object mutability becomes an issue here.
- If the object can be modified through one or more methods, it is mutable
- If you pass a mutable object into a method, that method may modify the object without your knowledge
- If the object is immutable, you can pass it into a method knowing the method cannot modify it.
- The most frequently used class, String, has no mutator methods so strings are immutable.
Objects as return values
Methods can return an object instead of a primitive value.
- Responsibilities of the method returning an object
- The object's class name must be given in the proper position of the method's signature
- Any "return" statements must include a reference to an object of that class (or a subclass)
- Responsibilities of the caller
- Place the method call in a position where an object of the expected type is needed
Example: Method that returns a string containing the sum of two integers. Code
representing the responsibilities is bolded.
public String sumString(int a, int b) {
String result = new Integer(a+b).toString();
return result;
}
Call it in a statement like System.out.println(sumString(39290,9923));
The Fraction class
The textbook's Fraction class includes all the above, so we studied it as a detailed example.
[ C
SC 160 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)