[Index
of weekly notes, Previous week, Next
week, All Notes]
ILS 614
(Winter 96/Weymouth)
Week 2: Objects, Classes, Methods
In this class we will dig deeper into the hows and whys of object oriented
programming. The goal is to provide concrete examples, using Java, of the
general concepts from the readings. At this point I assume that you have
read the introduction to object oriented programming and the detailed description
of the Java application from the notes of last week.
In this class we will cover the following:
- What is a class
- The first step in Object Oriented Design: Abstraction
- We design a geometric form class: the geoObject
class.
- Two types of forms: rectangle, and circle.
- Making object instances from classes
- Now you do it: The second assignment.
Summary of Readings
There is no required reading this week. If you do not get to the readings
for last
week, be sure to catch up.
Some documents of interest can be found at the URL http://www.javasoft.com/hooked/.
Assignment: The assignment for this week
is due at the start of class next week, on Thursday, January 25, 1996.
Introduction
Last week, in class and in the assignment, you were introduced to the concepts
of class, method, and object. Today, we will review these concepts and look,
in more depth, at exactly what they mean in the context of Java.
You goals for this week are:
- Understand these basic concepts: class, method, and object.
- Be able to create, compile, and run a multiple object "world"
in Java.
- Understand what is meant by inheritance.
- Turn in assignment 1 (if you have not already).
- Turn in assignment 2.
Before we start this week's exploration, lets look at last week:
- Things gone right, things gone wrong
- Intresting experiences
- Helpful hints.
Now for the topics...
What is a class
You tell me: What is a object class? Let's take a look as the first
at the code
for assignment one. In it, we defined a new class HelloWorldApp (or
whatever name you used) with one method (actually a method called a "class
method" for reasons that I explained in class).
Here is an example of a class definition in Java: class Circle
extends GeoObject { private int itsRadius; public Circle(int x, int y, int
radius) { super(x,y); itsRadius = radius; } public void printIt () { System.out.println("Circle:
base = (" + baseX + ", " + baseY + "), radius = "
+ itsRadius); } } The complete form of a class definition in Java
is: ClassModifiers class ClassName [extends superClass]
[implements interfaces] { /* list of class data fields */ /* list
of class methods */ }
ClassModifier can be any of the following modifiers, separated by
a space, in any order:
- abstract
- Contains abstract methods, that is, methods that are not implemented
in this class, and possibly other un-implemented parts.
- final
- No subclasses of this class can be made.
- public
- This class may be used by other classes outside of this class package.
Only one public class is allowed per file, and the file must be named className.java
- private
- This class can only be used within this file.
- --nothing--
- If no ClassModifier is given, this class is only accessible within the
current class package.
- synchronizable
- Access to objects of this class can be synchronized. (More about this
when we talk about Threads, much later).
Objects defined in Java belong to a package. Objects and methods
defined with a package can be protected from objects and methods outside
the package. For now the default package will suffice. The Java documentation
at Sun described how to create a package
and use it. We will talk more about packages in a future lesson.
In the example above, there is no ClassModifier; the name of the class being
defined is Circle; it is a subclass of a class named GeoObject;
and it has one data field, itsRadius, and two methods, Circle,
and printIt. The data field represents the radius of the circle.
The method printIt prints the values of data fields of the object.
The method Circle is the constructor method for the object. The
constructor method is a special method with the same name as the object.
It is required in any object class that is to be instantiated.
The first step in Object Oriented Design:
Abstraction
The class circle defines the features of circles that we are interested
in, for the sake of this example. If we wanted to add additional features,
for example, color, we would add a data field to the class definition, and
change the methods appropriately (for example, to print the color as well).
Instances, invocation, and other references to objects of this class that
did not deal directly with color would not have to be changed.
Methods represent actions that we might want to cause the object to take.
In this case the printIt method causes the object (or rather an
instance of the object) to print out its data fields.
The general form for a method in Java is: MethodModifiers ReturnType
Name (argType1 arg1, ...) { /* Body of the method */ }
MethodModifiers can be:
- public
- The method is accessible to all other methods.
- protected
- The method is only accessible to methods of the subclasses of its class.
- private
- The method is only accessible to other methods of its class.
- --nothing--
- The method is accessible to all other methods within classes contained
with the same package as this class.
- final
- The method can not be overridden (by the method of a subclass of the
class of this method).
- static
- The method is a class method. It is invoked with ClassName.methodName
and is shared by all instances of the class of this method.
- synchronized
- The method causes the object (in which the method "resides")
to be locked when the method is invoked. If the object is already locked,
the method causes invocation to wait until the lock is released. When the
lock is released, the invocation of the object continues.
- native
- A method which provides an interface to another language (usually C)
for access to functionality not available in Java.
Data fields are or the form FieldModifiers DataType FieldName;
The FieldModifiers are just the same as the MethodModifiers (from the definition
of a method) except for the modifiers final and synchronized.
The ReturnType of methods and the DataType of data fields can be any of
the basic Java data types: boolean, byte, short, char,
int, float, double, long; an array of any data
type; or any class that is defined. In addition ReturnType can be void
which means that no value is returned.
We will talk more about data types and operators next
week.
We design a geometric form class: the geoObject class.
You will notice, in our example, that the class Circle is a subclass
of the class GeoObject. GeoObject might be defined as this: class
GeoObject { protected int baseX; protected int baseY; public GeoObject (int
x, int y) { baseX = x; baseY = y; } public void printIt(){}; }
It is often an advantage, when you are defining a set of related classes,
to define those features that they have in common in a single class. The
advantage of this is that there is only one place to look for the implementation
of common methods and the definition of common data fields. Changes to the
common method are automatically part of any subclass of that method.
Let's look, again, at the printIt method of the class Circle.
This method invokes the system method (System.out.println) to print
a line. Such invocation is often referred to as "sending the object
a message," terminology borrowed from a computer language called SmallTalk.
Note that in the call to this print method (which, by the way, takes a string
as an argument, there is a reference to the data fields baseX,
and baseY which are not defined in Circle, but in fact
are defined in GeoObject. Every object in this world will have
some (x,y) point that we will call its base.
Two types of geometric forms: rectangle and circle.
Consider the entire example from the file GeoObject.java.
Some things to note. There are two subclasses of GeoOject; they are Circle
and Rectangle. They both define the method printIt. Thus, if I have an instance
of either type, let's call it myObject, it does not matter to me
which of the two types it is. I can invoke the method myObject.printIt()
and the appropriate method gets run.
Also note, the base point is defined in the class GeoObject, but used in
each of the subclasses.
Making object instances from classes
Just as in the first example, the execution of the program is done through
the public static method called main. In this case execution
consists of five steps.
- System.out.println("Starting GeoWorld");
- Print out a "here we are... starting up" type of message.
You know that your program is running.
- Rectangle r = new Rectangle(20,30,40,50);
- Create a new object. New ClassName(arg1, ...) is the way to define
a new object of the class ClassName; here we have defined a new object (class
instance of the class Rectangle). Note, also, that we have declared the
data item that refers to this object, r, as being of type Rectangle.
- Circle c = new Circle(60,70,80);
- Declare a new data item, c, of type Circle and initialize
it with a new object from the class Circle.
- r.printIt();
- Invoke the printIt method of the object r.
- c.printIt();
- Invoke the printIt method of the object c.
The program is actually just a method. The execution of all methods normally
proceeds line by line through the file. Normally, when a method is invoked,
the steps in that method are followed, line by line, until the method is
finished and then execution continues at the point where the method was
invoked. This line-by-line execution is called the flow of control; in other
words the "flow" of the control of the computer is determined
by the successive code lines in the method. Next
week, we will learn how to change the flow of control through the control
statements if, while, do, for, and switch.
Now you do it: The second assignment.
Your assignment is to extend the GeoObject
example. Obtain a copy of the file with the GeoObject example (you can
do this through your favorite web browser -- watch out for the file name).
Extend the example: define a new class called Square. The constructor
for square should take three arguments, the two values of the base point
and the size of one side. In other words, the constructor should be Square(x,y,s)
where x and y are the values for the base point and s is the size of a side
of the square. Implement a printIt method for the square. Modify
the comment field in the header of the file to reflect your ownership, the
new functionality, and your extensions. Compile it and run it. Email me
a copy of the code, and a (cut and past) dump of what was printed at the
terminal when you ran it. Save the source file, we will use this file as
the base for the next two assignments.
The url of this page is: http://www.sils.umich.edu/~weymouth/java/class/lectureNotes/lectureNotes02.html
Last modified on January 18, 1996.
Report spelling errors, corrections, suggestions, and additions to Terry
E Weymouth.
weymouth@umich.edu