Chapter 9


Exercise 1 Suppose package superpack contains subpackage subpack. Suppose a source file contains the following line:

import superpack.*;

Will this line import classes in subpack? Write code to support your answer.

Solution 1 Let's start by creating two classes, one in each package:

package superpack; public class InSuper { } -------------------------------- package superpack.subpack; public class InSub { }

These classes don't do anything, but they are all you need. You can store them in the same directory and compile by typing "javac -d . *.java", after which your directory looks like this:

click to expand

Now you can see what the import line does. Create another class in yet another package:

package testpack; import superpack.*; public class TestClass {   InSub x; }

The class declares a variable of type InSub. If the import line doesn't import contents of the subpackage, you should get a compiler error, because the compiler won't know what an InSub is. When you compile ("javac -d . TestClass.java"), you indeed get an error. This shows that importing "*" does not import subpackages.

Exercise 2 Create a class that illegally tries to read a private variable of another class. What is the point of this exercise?

Solution 2 First let's create and compile the class that owns the private variable:

class HasPrivate {   private int x; }

Now to try to access x:

class AccessX {   void tryIt()   {     HasPrivate hp = new HasPrivate();     hp.x = 10;   } }

When you try to compile this class, you get an error message that says something like

x has private access in HasPrivate

The point of this exercise, and the ones that follow, is to learn to recognize error messages that stem from misuse of the concepts in Chapter 9. This will make it easier to fix bugs when they crop up later on. Meanwhile, you're also getting good practice at thinking in terms of packages and class inheritance structures.

Exercise 3 Create a class that illegally tries to call a default-access method of another class.

Solution 3 Your first class will be called HasDefMethod:

package aaaaa; public class HasDefMethod {   void deffy()   {     System.out.println("deffy here.");   } }

The method may be called by any class in package aaaaa, so any illegal call attempt will have to come from a different package, like this:

package bbbbb; import aaaaa.HasDefMethod; public class BadCall {   void tryBadCall()   {     HasDefMethod h = new HasDefMethod(); // Ok     h.deffy(); // Won't compile   } }

The compilation error message says something like this:

deffy() is not public in aaaaa.HasDefMethod; cannot be accessed from outside package

Exercise 4 Create a class that illegally tries to write a protected variable of another class.

Solution 4

You need to create a subclass in a different package from its superclass. First, here's the superclass:

package aaaaa; public class HasProt {   protected double d; } And here's the subclass: package bbbbb; import aaaaa.HasProt; public class BadWrite extends HasProt {   void misuse()   {     HasProt other = new HasProt();     other.d = 3.14159; // Won't compile!   } }

The compilation error message is

d has protected access in aaaaa.HasProt

Since the subclass is in a different package from the superclass, an instance of the subclass may only access its own copy of a protected variable. In place of the line that doesn't compile, the following would be legal:

d = 3.14159;

Exercise 5 True or false: If a class has at least one abstract method, the class must be abstract. Write code to support your answer.

Solution 5 True. A class with any abstract methods must be abstract. The following class will not compile:

class NotAbstract {   abstract void abstactMethod(); }

Exercise 6 True or false: If a class is abstract, it must have at least one abstract method. Write code to support your answer.

Solution 6 False. It's okay for an abstract class to have no abstract methods. This isn't stated explicitly in the chapter, but it's easy enough to prove. The following class, which definitely doesn't contain any abstract methods, compiles without error:

Abstract class IsAbstract { }

Exercise 7 Write an application that tries to construct an instance of an abstract class. Can you compile the application? Can you execute it?

Solution 7 Here is an abstract class:

abstract class Ab { }

Here is an attempt to instantiate it:

class ConstructAbstract {   void constructInstanceOfAbstractClass()   {     Ab theInstance = new Ab();   } }

The compilation error message is something like this:

Ab is abstract; cannot be instantiated




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net