Chapter 7
From Object-Oriented Designs to API’s
7.1 Basic Designs
- Code each class:
- Name
- Braced body, after the name
- The ‘class’ keyword, before the name
- Optional modifiers, before the ‘class’ keyword
- Code each field:
- Name
- Type specification, before the name
- Optional modifiers, before the type specification
- Semicolon, after the name
- Code each method:
- Name
- Type specification, before the name
- Optional modifiers, before the type specification
- Parenthesized arguments, after the name
- Braced body, after the parentheses
- Code each constructor:
- Name, identical to that of the class
- Braced body, after the name
- Parenthesized arguments, after the name
- Braced body, after the parentheses
class Bulb {
boolean status;
Bulb () { }
void flip (){ }
}
7.2 Private and Shared Features
- A class is a description for a set of objects
- Fields and methods
- Might be ownd by each instance of the class.
- Might be commonly shared by all the instances.
- The shared features are marked as ‘static’ in java, in the modifier parts.
- Static fields and methods are said to be class method and features.
- Non-static fields and methods are said to be instance method and features.
- Constructors are shared features, coded without the static modifier.
| Bulb |
status average |
flip flipAll |
class Bulb {
boolean status;
/** average life in hours per bulb */
static int average;
Bulb () { }
/** a switch for each bulb */
void flip (){ }
/** a centeral switch for all the bulbs */
static void flipAll (){ }
}
7.3 Inheritance
- Designs might be for sharing by more than one class
- Features shared by different classes might be moved to a base class, to be extended by the other classes
- The extension classes should identify their base classes.
- Enter the keyword ‘extends’, immediate after the extension class name
- Enter the base class name, after the keyword
class Bulb {
boolean status;
Bulb () { }
void flip (){ }
}
class NeonBulb extends Bulb {
double length;
}
class regularBulb extends Bulb {
double radius;
}
7.4 Assignment #8: Programming APIs
Due: We, Apr 23, midnight
For each of our Clock, Car, and Pet designs, prepare a file which defines the relevant APIs. The files should be
compilable under javadoc -package into web pages that list the different features together with appropriate
comments.
| Clock |
hr min sec |
set tick showTime |
| Car |
gas consumption |
fill() fill(i) drive(x) |
Assume ‘lab8’ for the submit program.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A