Java's Object Mechanisms

I. Inheritance

    A. You create a subclass using the extend keyword

    B. Access Modifiers: more on these later when packages are introduced

        1. public
	2. protected
	3. private

    C. Constructors

        1. use super to call the superclass constructor
	2. super must be the first statement in a constructor
	3. if super is omitted then the superclass's default 0-argument
	    constructor is called
	4. constructors are called in order of derivation, from superclass
	    to subclass
	5. super can be used to access any member of a superclass. It is
	    commonly used to access the superclass's method when you are
	    overriding it in the subclass.

    D. All methods are virtual by default

        1. Use the final keyword to prevent a method from being

	2. abstract methods with no method body can be created by prefixing
	    the method name with the abstract keyword

	3. classes that contain abstract methods must be declared abstract

	    a. subclasses must be declared abstract if they fail to define
	        an abstract method that they inherit from an abstract 

    E. Object class: Implicit superclass for any class that is not defined
        to extend another class--all objects can be cast to Object

II. Packages 

    A. What they are: Packages provide a way of grouping together a
	   set of related classes as one unit

        1. Packages act like a module in that they provide access control
	       mechanisms (more on this in a moment)

        2. Packages help partition the name space so that names can
	       be re-used within different packages

    B. Defining a package

	   a. Use the package keyword to indicate that a class belongs
	      to a certain package

	   b. The package statement must be the first executable statement
	      in a file

	   c. Packages can be nested. If they are nested, then you separate
	       a package name from the one above it by a period.

	       Example: package silhouette;
	                package silhouette.shapes;
			package silhouette.shapes.boxobjects;

    C. Importing Packages

	1. Use the import keyword to access a public class from another

	    a. Specifying the name of a class will import that class only
	    b. Specifying an asterisk (*) after a package name will import
	       all public classes from that package

	       example: import java.util.LinkedList -- makes the LinkedList
	                    class accessible
			import java.util.* -- makes all public classes in
			    java.util accessible
	    c. The import statement places the classes returned by the 
	       statement into the namespace of the current file.
	       A namespace is the set of names that are defined in the
	       current scope. A scope is a block of code within which
	       a set of names are valid. A scope can be a file, a class, 
	       a method or a block.
            d. You can also access a class in a different package without
	       importing it. To do so you provide the fully qualified package
	       name for the class. For example:

	       java.util.LinkedList myList = new java.util.LinkedList();

	       You might decide to use the fully qualified name if importing
	       the name would cause a conflict with either a name declared
	       by your class or with a name imported from another package.

        2. import statements should go immediately after a package statement
	   and should precede other executable statements in your file.

	3. Java's libraries are contained in packages. 

	    a. In C++ you use #include's to include system-defined libraries
	       in your program

	    b. In Java you use import statements to include system-defined
	       libraries in your program
	       Example:    C++                    Java
	                   #include <stdio.h>     import*;

    C. How Java finds classes and CLASSPATH

	    a. The directory containing a package must have the
	       same name as the package

	    b. All classes belonging to the package must be placed in the
	       package's directory

	    c. How Java locates class files

	        i. By looking at directories beneath the current directory
		ii. By examining the set of directories specified by the
		    CLASSPATH environment variable 

		    Example: setenv CLASSPATH .:..:/sunshine/homes/bvz/cs365

		    Using this CLASSPATH variable, Java will search the
		    current directories, the directory above the current
		    directory, and /sunshine/homes/bvz/cs365 for the names
		    of package directories. If I wanted to execute a 
		    file in package silhouette, then silhouette needs to
		    be a subdirectory of one of the directories specified
		    by the CLASSPATH variable

	    d. When executing a java class, the class must be prefixed by
	        its full package name

		Example: java silhouette.shapes.boxobjects.rectangle

     D. Member Access
                                        Private  Default   Protected   Public
					Member   Member    Member      Member

         Visible within same class       Yes      Yes        Yes        Yes

	 Visible within same package     No       Yes        Yes        Yes
	    by subclass

	 Visible within same package     No       Yes        Yes        Yes
	    by non-subclass

	 Visible within different        No       No         Yes        Yes
	    package by subclass

	 Visible within different        No       No         No         Yes
	    package by non-subclass

III. Interfaces

    A. An interface specifies a set of methods that a class agrees to
       implement. An interface does not provide an implementation for these

       1. Similar to an abstract class

       2. Classes can choose to implement as many interfaces as they desire

       3. Syntax: access interface {
                     return-type method-name1(param-list);
		     return-type method-name2(param-list);
		     type var1 = value;
		     type var2 = value;
		     // ...
		     return-type method-nameN(param-list);
		     type varM = value;

       Example: public interface Iterator {
                    public boolean hasNext();
		    public Object next();
		    public void remove();
 	   a. access is either public or default (i.e., package)
	   b. all methods/variables must be declared public

       4. Interfaces can extend other interfaces

    B. Implementing Interfaces: A class that implements an interface must
       declare and define each of the interface's methods and must declare
       those methods public, regardless of whether they are declared public
       in the interface declaration

       1. Use the implements keyword

       2. Specify multiple interfaces by providing a list of names separated by

    C. Variables can be declared to be of an interface type

    D. Variables in interfaces provide a way of declaring a set of global,
    	read-only variables that will be used by many of the classes in an 

       1. Whenever you would declare a global constant in C++, 
          place a similar variable into an interface in Java.

       2. Classes that want to reference these global variables will implement
          the interface
	  Example: public interface GlobalVariables {
	             int MIN = 0;
		     int MAX = 10;
		     string BADOPERATOR = "Error: Bad Operator";

		   public class Node implements GlobalVariables {
		      int minValue = MIN;