Week 7 - Objects, Interfaces, Packages
Recommended Reading:
Make sure you read Objects
and Classes in Java from the tutorial to see how OO is done in Java.
Java OO Fundamentals
Objects
When we refer to "objects" in Java, we are talking about "classes"--Just
like in C++.
OO concepts and how they are done in Java:
- Inheritance is done with the keyword extends. This
is the is-a relationship. Every class ever made in Java will
inherit either directly or indirectly from a "root" class called (you
guessed it) Object. If you want to make a method/member
non-inheritable, give it the final specifier. This is the
counterpart to the const keyword in C & C++.
- Encapsulation is done by simply declaring a primitive or
object as a member of a class. This is the has-a relationship.
- Polymorphism is done by deriving an object from a parent
and creating a method which has the same name as the parent. In Java,
unlike C++, all methods are implicitly "virtual". It is considered good OO
programming to use Polymorphism rather than type-checking (i.e. in the
form of an if-then-else series of statements).
- Templates There are no templates in Java. They are
unneccesary because all classes are derived from Object.
Basically, you cast classes as Object when storing them in a
"generic" collection (or some such). Then, when you retrieve them, you
call getClass() to determine what kind of class it is and re-cast
it as it's original object.
- Access Types Just like C++, Java has access types which
specify the visibility of methods (functions) and members (variables).
There is a public and a private specifier, but no
protected specifier. All "naked" methods/members are implicitly
protected, with the additional twist that they are "public" to any other
class in the same package.
- Class Methods/Members Anything with a static
decl-specifier is a class method/member. This means that the
method/member is "global" to all instances of that class. If the static
keyword is not present, the method/member is only usable/visible on a
per-instance basis.
- The "Uses" Relationsip As mentioned above, the is-a
and has-a relationships are two types of ways that two types of
objects can relate to each other. The last is the uses
relationship. This means that object A is passed as an argument to a
method of object B.
- Constructors Constructors in Java work just like they do in
C++ with one exception: There is no "off-the-stack" construction; all
objects begin their lives as references and must be constructed with the
new operator.
- Destructors There really is no need for destructors in Java
like there is in C++ because memory deallocation is done automatically via
garbage collection. There is, however, a finalize() method that
is called when an object "dies".
Interfaces
Interfaces are how Java addresses the issue of multiple-inheritance. This
is because there is no multiple-inheritance in Java in the traditional sense.
Things to remember about interfaces:
- An interface can inherit from another interface, but not from a class.
- A class can use more than one interface (using the implements
keyword).
- All methods in an interface must have an override in the class that
implements them.
- Interfaces cannot have member variables, only methods. (I think)
Packages
Packages are Java's approach to creating re-usable modules. A package is
included in a program via the import keyword. This is analagous to
the #include directive in C. One package that is always included by
default is java.lang.
To write packages of your own, simply type the keyword package at
the top of a file containing a number of classes that you wish to re-use. A
package can span multiple files. Each file that is part of a package will need
the line package package-name near the top of the file.
If you want to look at the Java source code for the packages that come with
the JDK, they are found in the directory /usr/local/java/src.
Some Practical Java Objects
Applets
Some useful methods in Applets:
- init - Called when the applet is first loaded.
- start - Called whenever the applet is loaded in a browser (i.e.
you go away and come back, so start gets called.
- stop - Called whenever you leave the page containing an applet.
- paint - Called whenever your applet needs to be "drawn".
Look at the section in the tutorial about Writing
Applets for more info. Plan on some of the stuff in here being on a test.
Threads
If we can get around to talking about these, that would be cool.
Otherwise, look here for
info on using threads.
Other Miscellany
Look at the Essential
Java Classes page on the tutorial.
Check out the following package specifications for some useful,
general-purpose objects:
Changelog
2/25/99 - Initial revision, in the afternoon, changes made at night.
3/01/99 - Added 'Essential Java Classes' link and 'Recommended Reading'.