Problem Solving and Computing (CSC-103 98S)
Outline of Class 18: Java's Object Model
Held: Thursday, April 2, 1998
Today each person will present the work they've done (hopefully).
- In Java, you program by defining classes, constructing instances of
(objects from) those classes, and doing things with those instances.
- Each class is stored in a separate file.
- What are the components of a class? (Not in order.)
- The class name
- The protection level of the class (for now, we'll just use
- The body of the class, which includes
- attributes (also called fields), which
are then customized for each instance of the class
- methods, which may behave differently for
each instance of the class
- class attributes, which belong to the class
as a whole, rather than to any individual members
- class methods, which belong to the class as a
whole, and are not associated with individual members
- constructors, which describe how to build
instances of the class
- destructors, which describe how to get rid of
instances of the class
- How do you define a class?
- A class definition begins with the keyword
the name of the class. Custom says that we capitalize the names
- The body of the class begins with a left brace, and
ends with a right brace.
- Attributes are
declared by writing the class (or primitive type) of the
attribute, followed by the name of the attribute,
followed by a semicolon.
- Java allows you to restrict access to classes. To make a
class generally accessible, you precede the keyword
- Java allows you to base classes on other classes. We'll get
back to this later.
- Java allows you to indicate that your class will support particular
sets of actions. We'll get back to this later.
- Attributes can also have protection modifiers (like the
public we use with the class).
- The "primitive types" mentioned above are the primary building blocks
of objects. They are the basic kinds of values that the Java machine
knows about, and include
boolean, for true/false values
char, for characters (unlike many other languages,
Java officially supports more than just the basic American
set; it has built-in support for full Unicode)
byte, for very small integers in the range -128 to
127 (integers are numbers without fractional portions)
short, for small integers in the range -32,768 to 32,767.
int, for integers in the approximate range -2 billion to
long, for integers in a much bigger range
float, for numbers that may have a fractional
component (e.g., 10.32533).
double, for better accuracy
- The names are composed of letters, numbers, and underscores (and
perhaps a few other things) and must begin with a letter or
- In effect, the fields of a class are variables that can be shared
by every method in the class.
- Note that I may not include all these comments in my sample code, but
you should plan to.
- How do you get a new object from this class? Traditionally, you
can declare a new object with
class-name variable-name = new class-name()
- For example,
Point pt = new Point()
- To define a method of a class, you provide
- The return type of the method (you return values from methods
- The name of the method
- An open paren
- The list of parameters to the method
- A close paren
- An open brace
- The body of the method (a series of statements, each of which
is terminated by a semicolon)
- A close brace
- You can also preface the return type with a number of modifiers,
which we'll come back to later.
- Standard methods can refer to the attributes associated with the
class (and ancestor classes, but we'll get back to that later).
- To call the method associated with an object, you use
- the object's name
- a period
- the method's name
- an open paren
- the paremeters to the method
- a close paren
- Note that this is somewhat different than in a language like C or
Pascal, in which the object would be one of the parameters.
- Here's our sample class, extended with three methods, one to set the
parts, one to get the real part, and one to get the imaginary
* A simple representation of points.
* @author Samuel A. Rebelsky
* @version 1.2 of April 1998
public class Point
// | Attributes |
* The x position of the point.
protected double xpos;
* The y position of the point.
protected double ypos;
// | Modifiers |
* Set the location (value) of our point.
public void setLocation(double x, double y)
xpos = x;
ypos = y;
} // setLocation(double, double)
// | Accessors |
* Get the x component of this point.
public double getX()
} // getReal
* Get the y component of this point.
public double getY()
} // getImaginary
} // Point
- Along with creating new objects, we may need to initialize the objects
(assigning values to particular fields, allocating new objects for some
- Classes can include constructors that describe how to create
- Constructors can have parameters that are used in the initialization
of the object.
- Constructors look very much like methods, with the difference that they
have the same name as the class.
- Java is something of a polymorphic language: it permits you
to define multiple methods or constructors with the same name, as long
as they have different parameter types.
- This is particularly important for constructors, as there are many
different ways that we may want to describe a new object.
- For our sample
Point class, we'll have two constructors:
- A default constructor, with no parameters. This will create
a new point at (0,0).
- A constructor that takes both x and y values.
- Here are the two constructors:
* Initialize to (0,0);
xpos = 0.0;
ypos = 0.0;
} // Point()
* Initialize to (x,y)
public Point(double x,double y)
xpos = x;
ypos = y;
} // Point(double,double)
- If you don't declare any constructors, Java uses a default constructor
with no parameters.
- If you do declare constructors, that default constructors is no longer
- When building classes for this course, you should always define
- Java is much more restrictive on using members of superclasses in
place of members of subclasses.
- Why do you think Java has these restrictions?
- Note that if you do not provide an
Java assumes that your object extends
- Java permits only single inheritance -- a particular class
can only directly derive from one other class.
- In the alternative, multiple inheritance -- a
particular class can directly derive from more than one
- Single inheritance makes analysis and design much easier.
Otherwise, there are potential
problems with the same method name being defined in both
- Java provides interfaces to support many of
the needs of multiple inheritance.