Starting from:

$19

Chapter 13 - Interfaces and Inner Classes

Exercise 1: Can you have a variable of an interface type? Can you have a parameter of an interface type? Exercise 2: Can an abstract class ever implement an interface? Exercise 3:Can a derived class have two base classes? Can it implement two interfaces? Exercise 4: Can an interface implement another interface? Exercise 5: The method interchange in Display 13.5(see ComparableDemo in my Eclispe) makes no use of the fact that its second argument is an array with base type Comparable. Suppose we change the parameter type Comparable[] to Object[] and change the type of the variable temp to Object. Would the program in Display 13.6 produce the same dialogue? Exercise 6: Is the following a suitable implementation of the Comparable interface? Exercise 7: Suppose you have a class Circle that represents circles which all have centers at the same point.(To make it concrete, you can take the circles to be in the usual x,y plain and to all have their centers at the origin.) Suppose there is a boolean valued method inside of the class Circle such that, for circles c1 and c2. Exercise 8: Will the following program compile? If it does compile, will it run? Interface1 and Interface2 are defined below. Exercise 9:Will the following program compile? If it does compile, will it run? Interface1 and Interface2 are defined in Exercise 8. Exercise 10: Will the following program compile? If it does compile, will it run? InterfaceA and InterfaceB are defined below. Exercise 11: Will the following two interfaces and the following program class compile? If they compile, will the program run with no error message? Exercise 12: Modify the following class definition so it correctly implements the Cloneable interface(all the instance variables are shown): Exercise 13: Modify the following class definition so it correctly implements the Cloneable interface (all the new instance variables are shown): Exercise 14: Modify the following class definition so it correctly implements the Cloneable interface(all the instance variables are shown): Exercise 15: Modify the following class definition so it correctly implements the Cloneable interface(all the new instance variables are shown): Exercise 20: Would the following invocation of getAmount in the method getBalance of the outer class BankAccount still be legal if we change the method getAmount of the inner class Money from public to private? Exercise 21: Because it doesn't matter if we make the members of a private inner class public or private, can we simply omit the public or private mififiers from the insatnce variables and methods of a private inner class? Exercise 22:Would it be legal to add the following method to the inner class Money ? Remember, the question is would it be legal, not would it be sensible. Exercise 23: Would it be legal to add the following method to the inner class Money? Remember, the question is would it be legal, not would it be sensible. Exercise 24: Can you have a static method in a non-static inner class? Exercise 25: Can you have a non-static method in a static inner class? Exercise 28: Suppose we replace NumberCarrier anObject with Object anObject in Display 13.11. What would be the first statement in the program to cause an error message? Would it be a compiler error message or a run-time error message?

More products