Bruce Eckel's Thinking in Java Contents | Prev | Next

Exercises

  1. Create a class with public, private, protected, and “friendly” data members and method members. Create an object of this class and see what kind of compiler messages you get when you try to access all the class members. Be aware that classes in the same directory are part of the “default” package.
  2. Create a class with protected data. Create a second class in the same file with a method that manipulates the protected data in the first class.
  3. Create a new directory and edit your CLASSPATH to include that new directory. Copy the P.class file to your new directory and then change the names of the file, the P class inside and the method names. (You might also want to add additional output to watch how it works.) Create another program in a different directory that uses your new class.
  4. Create the following file in the c05 directory (presumably in your CLASSPATH):
//: PackagedClass.java
package c05;
class PackagedClass {
  public PackagedClass() {
    System.out.println(
      "Creating a packaged class");
  }
} ///:~ 

Then create the following file in a directory other than c05:

//: Foreign.java
package c05.foreign;
import c05.*;
public class Foreign {
   public static void main (String[] args) {
      PackagedClass pc = new PackagedClass();
   }
} ///:~ 

Explain why the compiler generates an error. Would making the Foreign class part of the c05 package change anything?

k

Contents | Prev | Next