Exercises

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

  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



Comments

  • genesis4iphone icon

    Posted by wpicoinmaker.cxodsple..xcom on 12/12/2012 01:58pm

    By WebOsPublisher . VisionWalk Facebook Icons - Foundation Fighting Blindness Share Share on Facebook [removed](unescape(ID13b798b23d4getTitle()).replace(/\n/g, '')); Share Cancel Share on Yahoo! Share Cancel Share on MySpace Share Cancel Share on Twitter A short URL will be added to the end of your Tweet. Share Cancel Share on LinkedIn Share Cancel Close Finish Watch Facebook Icons To save an icon below: Right click on the icon of your choice and select "Save Picture As" from the drop down list, give it a name, decide where you would like to save it and click "Save". To set the icon as your profile picture in Facebook: Click on your current profile picture and click on "Change Profile Picture" in the upper right hand corner of the picture. In the window that opens click "Browse" and find the location where you saved the icon earlier and click "Open" in the browser window. Frequently Asked Questions | Contact Us | Foundation Fighting Blindness Home Page | VisionWalk Home Page | Privacy Policy

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds