HELP! * YELLOW=global GREY=local Full HTML for

GLOBAL foilset Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects

Given by Nancy McCracken at CPS616 spring 1997 on Jan 27 1997. Foils prepared 1 February 97
Abstract * Foil Index for this file Secs 41.7 See also color IMAGE

Basic Object Structure of Java with general words for those unfamiliar with concept
Classes and Methods
Interfaces done briefly
Packages
Exceptions done quickly

Table of Contents for full HTML of Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects


1 CPS 616 Java Lectures
with Audio January 27 97
Objects -- Methods Interfaces etc.
See:
http://www.npac.syr.edu/users/gcf/cps616-97jan27

2 Abstract of CPS616-97 Lecture of January 27
3 The Java Object Model Overview
4 API of a Class
5 Using a Class
6 A Computational Class
7 Class Definitions
8 Constructors of Objects
9 Java Language -- Types of Classes - I
10 Java Language -- Types of Classes - II
11 Java Language -- Types of Methods
12 Relationships between Classes
13 Use of Methods Defined in Parent
14 Use of Methods Defined in Parent but overridden in child class
15 Comments on Casting
16 Array - A Pseudo Class!
17 By value and By reference
18 Basic Class Operations
The Class Hierarchy of this example

19 Object-Oriented Programming in JAVA - defining a class
20 More methods in the class definition
21 Defining a child class
22 Using parent and child classes
23 Parent and child example, continued
24 Overloading Constructors
25 More child classes
26 Java Language -- Interfaces - Overview
27 Overview of Packages and Directory Structure
28 Using Java packages
29 Java System Packages
30 Basic Structure of Exception Handling in Nested Calls
31 Examples of Exception Hierarchy

This table of Contents Abstract



HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 1 CPS 616 Java Lectures
with Audio January 27 97
Objects -- Methods Interfaces etc.
See:
http://www.npac.syr.edu/users/gcf/cps616-97jan27

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 41.7 Full HTML Index
Instructor: Nancy McCracken
teamed with Meryem Ispirli, Geoffrey Fox,
Tom Scavo, John Yip
Syracuse University
111 College Place
Syracuse
New York 13244-4100

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 2 Abstract of CPS616-97 Lecture of January 27

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 41.7 Full HTML Index
Basic Object Structure of Java with general words for those unfamiliar with concept
Classes and Methods
Interfaces done briefly
Packages
Exceptions done quickly

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 3 The Java Object Model Overview

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 322.5 Full HTML Index
Programs are composed of a set of modules called classes. Each class is a template
specifying a set of behaviors on
the data of the class.
Each class has class variables
(sometimes called instance vars)
to hold the data and methods (called
functions or procedures in other
languages) define the behaviors.
Each object in a program is created
as an instance of a class. Each class
instance has its own copy of the class
variables.
Classes can be used for data encapsulation, hiding the details of the data representation from the user of the class.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 4 API of a Class

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 272.1 Full HTML Index
Each class has an API (Application Programming Interface) consisting of all the variables and methods that other programmers (i.e. in other classes) are allowed to use. These are designated by the "public" keyword.
Example showing part of the Java Date class:
  • public class Date
  • { // Constructor methods to create instances of class
    • public Date();
    • public Date(int year, int month, int day);
  • // Accessor and Mutator methods to access and change data
    • public int getHours();
    • public int getYear();
    • . . .
    • public void setHours();
    • public void setYear();
    • . . .
  • // Other public methods
    • public boolean after(Date when);
    • . . . }
The on-line Java Hierarchy and Index shows the API's of all Java classes.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 5 Using a Class

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 230.4 Full HTML Index
This declares object today to have type class
    • Date today
Date() is Constructor of Date class which constructs an instance of Date class and sets default value to be the current date
    • new Date()
An example application using a method of the Date class:
  • import java.util.Date;
  • class DateApplication
  • { public static void main ()
  • { Date today = new Date();
  • Date medieval = new Date(1400, 12, 25);
  • if (today.after (medieval))
    • System.out.println( "Today is not medieval!");
    • }}

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 6 A Computational Class

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 106.5 Full HTML Index
A class may also be used to have just one computational instance.
This application reads from standard input and counts number of characters which are then printed
class Count {
public static void main (String args[])
throws java.io.IOException
{ int count = 0;
  • while (System.in.read() != -1)
  • count++;
  • System.out.println("Input has " + count + " chars.");
}}

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 7 Class Definitions

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 204.4 Full HTML Index
Class declaration in Java shares common aspects with C++ but there are also some syntactic and semantic differences.
Only single inheritance is supported but aspects of multiple inheritance can be achieved in terms of the interface construct. Interface is similar to an abstract class with all methods being abstract and with all variables being static (global). Unlike classes, interfaces can be multiply-inherited.
ClassModifiers class className [extends superClass] [implements interfaces] {
e.g. public class Test extends Applet implements Runnable {
defines an applet that can use threads which have methods defined by Runnable interface

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 8 Constructors of Objects

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 67.6 Full HTML Index
Methods of identical name to class are special -- they are constructors
mPoint() { /* Default Constructor */
  • x=0;
  • y=0;
}
We can overload method to define different versions with different numbers and types of arguments
mPoint(int _x, int _y) { /* Constructor to set Specific Initial values */
  • x = _x;
  • y = _y;
}
mPoint can have other methods to set its instance variables such as:
public void setDelta(int _dx, int _dy) {
  • dx = _dx;
  • dy = _dy;
}

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 9 Java Language -- Types of Classes - I

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 96.4 Full HTML Index
Possible ClassModifiers are:
abstract -- Contains abstract methods without implementation -- typically such abstract classes have several subclasses that define implementation of methods
public -- May be used by code outside the class package and (unix) file must be called ClassName.java where ClassName is unique public class in file
private -- this class can only be used within current file -- i.e current class
friendly(i.e. empty ClassModifier) -- class can be used only within current package
protected -- Only accessible to subclasses

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 10 Java Language -- Types of Classes - II

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 10 Full HTML Index
threadsafe: Instance or static variables will never change asynchronously and so can use compiler optimizations such as assigning to registers. Next modifier -- final -- is also valuable to compilers
final -- Cannot have a subclass for classes
  • cannot be overridden for methods
  • final variables have a constant value e.g.
    • final int ageatdeath = 101;
transient -- specifies that objects are not persistent
Note most of these modifiers can be used either for a class or an object -- a particular instance of a class
  • abstract only makes sense for a class and transient is perhaps more useful on an object basis

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 11 Java Language -- Types of Methods

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 120.9 Full HTML Index
MethodModifier ReturnType Name(argType1 arg1, ......)
Returntypes are either simple types (int, byte etc.), arrays or class names
Possible MethodModifiers are:
  • public -- This method is accessible by all methods inside and outside class
  • protected -- This method is only accessible by a subclass
  • private -- This method is only accessible to other methods in this class
  • friendly(i.e. empty) -- This method is accessible by methods in classes that are in same package
  • final -- a method that cannot be overriden
  • static -- This method is shared by ALL instances of this class and can be invoked with <Class>.method syntax as well as <Instance>.method
  • synchronized -- This method locks object on entry and unlocks it on exit. If the object is already locked, the method waits until the lock is released before executing -- can be used on methods or statement blocks
  • native -- to declare methods implemented in a platform -- dependent language, e.g. C.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 12 Relationships between Classes

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 138.2 Full HTML Index
use
  • A uses B: A calls a method (sends a message to) and object of B or creates, receives, or returns an object of B.
containment
  • A has a B: special case of use - an object of class A contains an object of B
inheritance
  • B is a A: specialization - B extends A (is a subclass of A) if B has all the variables and methods of A (and more).
  • In the class definition of B, there is no need to repeat declarations of variables and methods of A, they are assumed to be there. The definition of B has the additional variables and methods of B.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 13 Use of Methods Defined in Parent

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 192.9 Full HTML Index
Call to method in Object2 (message) from object1 is passed up the class hierarchy until a definition is found

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 14 Use of Methods Defined in Parent but overridden in child class

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 76.3 Full HTML Index
Call to method in Object2 (message) from object1 is passed up the class hierarchy until a definition is found

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 15 Comments on Casting

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 90.7 Full HTML Index
Casting (type conversion) is supported between types and class types. Syntax:
  • (classname)reference
Two forms of casting are possible: widening and narrowing
Widening, where the subclass is used as an instance of the superclass, is performed implicitly
Narrowing, where the superclass is used as an instance of the subclass, must be performed explicitly
Given Parent: Dot -> DrawableDot (Child):
  • Widening: An instance of DrawableDot is used as an instance of Dot
  • Narrowing: An instance of Dot is used as an instance of DrawableDot
Casting between sibling classes is a compile-time error

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 16 Array - A Pseudo Class!

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 57.6 Full HTML Index
Not in any package
One final instance variable: length
For each primitive type (and all classes), there's an implicit Array subclass
Cannot be extended (subclassed)
Superclass is Object
Inherits methods from Object
(new int[5]).getClass().getSuperclass()
will return Java.lang.Object

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 17 By value and By reference

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 141.1 Full HTML Index
Many languages are confusing as they differ in often unstated distinction between the value and "handle" -- Java is no exception! (reference,address,pointer) of an entity
Consider assignment: a = b; // sets value of a to value of b
If a and b are primitive types, then they hold "actual literals" and so if b=66, then a is set to 66
  • In this case if you change b, then a is left unchanged
However if a or b is an object, b is in fact a reference and so one sets a to refer to same object as b (i.e. same "location" in memory)
  • if you change b in some way, then a will be changed accordingly
  • Note null is value of an object which has not been assigned (constructed) and so does not point anywhere
Arguments to Methods are always passed by value BUT if an object is an argument, then that value is ALWAYS a reference and so in practice
  • Primitive types are passed by value
  • Objects are passed by reference
Arrays reflect properties of what they are arrays of!

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 18 Basic Class Operations
The Class Hierarchy of this example

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 339.8 Full HTML Index

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 19 Object-Oriented Programming in JAVA - defining a class

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 110.8 Full HTML Index
We define a parent class for movable point objects. Each instance of this class is represented by an x,y location, by a dx,dy offset for the object to move, and a color for the object. This example also illustrates data encapsulation, where users of the class must use methods to get data of the class.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 20 More methods in the class definition

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 128.1 Full HTML Index
We include methods to change the default values for the offsets and color, a method to move the object, and one to paint a point.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 21 Defining a child class

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 383 Full HTML Index
We define a class for movable rectangle objects. It uses x,y from the parent class mPoint for the location of the rectangle, but adds h,w to specify height and width of the rectangle.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 22 Using parent and child classes

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 702.7 Full HTML Index
This applet creates 2 mRectangles and loops to move them.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 23 Parent and child example, continued

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 142.5 Full HTML Index
The repaint method calls update, which is overridden here to move the objects, and paint, which is overridden here to redraw whole graphics area.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 24 Overloading Constructors

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 17.2 Full HTML Index
You are allowed to have more than one constructor in a class, if the constructors have different types or numbers of arguments. For example, for people who are using the java.awt.Point class, you can add a constructor to mRectangle which creates a rectangle by giving the top left and bottom right corner points.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 25 More child classes

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 97.9 Full HTML Index
Once we have structured the concepts of our data into classes, it is easy to add new shapes of movable objects as child classes of mRectangle. These classes only have to redefine paint to draw a differently shaped object.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 26 Java Language -- Interfaces - Overview

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 61.9 Full HTML Index
An interface specifies a collection of methods (behaviors) without implementing their bodies (akin to giving the API).
  • public interface Storable {
    • public abstract void store(Stream s);
    • public abstract void retrieve(Stream s);
    • }
Any other class which implements the interface is guaranteeing that it will have the set of behaviors, and will give concrete bodies to the methods of the interface.
Interfaces solve some of the same problems as multiple inheritance, without as much overhead at runtime.
  • There is a small performance penalty because interfaces involve dynamic method binding.
Interfaces can be implemented by classes on unrelated inheritance trees, making it unnecessary to add methods to common superclass.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 27 Overview of Packages and Directory Structure

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 36 Full HTML Index
One file can contain several related classes, but only one of them can be public. If the public class is called wheat.java, then the file must be called wheat.
A set of classes in different files can be grouped together in a package. Each of the files must be in the same directory and contain the command
  • package mill;
The name of the directory must also be mill.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 28 Using Java packages

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 18.7 Full HTML Index
One conveniently uses files in a package by inserting
  • import mill.*
at the beginning of a file that needs classes from the mill package
  • Then classes in the mill package can be refered to by just using their Classname
  • Without the import command, one must explicitly say mill.Classname
Packages can be grouped hierarchically, with the corresponding directory tree. For example, the mill package could be a subpackage of agriculture. Then a class is referred to as agriculture.mill.Classname.
Except for classes provided with the Java language, all of which have the form java.X, a class that is imported or used must either be in the current directory or be accessible to the compiler through the CLASSPATH environment variable.

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 29 Java System Packages

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * See also color IMAGE
Secs 76.3 Full HTML Index
java.lang Contains essential Java classes and is by default imported into every Java file and so import java.lang.* is unnecessary. Thread, Math, Object and Type Wrappers are here
java.io contains classes to do I/O. This is not necessary (or even allowed!) for applets which can't do much I/O in Netscape!
java.util contains various utility classes that didn't make it to java.lang. Date is here as are hashtables
java.net contains classes to do network applications. This will be important for any distributed applications
java.applet has the classes needed to support applets
java.awt has the classes to support windowing -- The Abstract Windows Toolkit
java.awt.image has image processing classes
java.awt.peer is a secret set of classes with platform dependent details

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 30 Basic Structure of Exception Handling in Nested Calls

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 66.2 Full HTML Index
method1 {
  • try {
  • call method2;
  • } catch (Exception3 e) {
  • doErrorProcessing(e);
  • }
}
method2 throws Exception3 {
  • call method3; // method2 just passes exception through
}
method3 throws Exception3 {
  • call dividebyzeroorreadfileorsomething; // create exception
}

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 1 February 97

Foil 31 Examples of Exception Hierarchy

From Jan 27 Delivered Lecture for Course CPS616 -- Java Lecture 2 -- Basic Applets and Objects CPS616 spring 1997 -- Jan 27 1997. * Critical Information in IMAGE
Secs 21.6 Full HTML Index
As Examples of hierarchy:
catch(InvalidIndexException e) {..} would catch particular exception whereas
catch(ArrayException e) {..} would catch all Arrayexceptions

Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Feb 16 1997