I'm reading chapter 8 in Thinking In Java and am trying to understand the difference between local inner classes that use the default constructor vs those that don't. In example 1, the book says that the local inner class piece translates into this...
class MyContents implements Contents {
privateint i = 11;
publicint value( ) { return i; }
}
returnnew MyContents( );
My question, however, relates to example 2. What would the "translation" of the local inner class in that example be. Would it be the following?
//: c08:Parcel6.java
// A method that returns an anonymous inner class.
publicclass Parcel6 {
public Contents cont() {
returnnew Contents() {
privateint i = 11;
publicint value() { return i; }
}; // Semicolon required in this case
}
publicstaticvoid main(String[] args) {
Parcel6 p = new Parcel6();
Contents c = p.cont();
}
} ///:~
-------------------------------
//: c08:Parcel7.java
// An anonymous inner class that calls
// the base-class constructor.
publicclass Parcel7 {
public Wrapping wrap(int x) {
// Base constructor call:
returnnew Wrapping(x) { // Pass constructor argument.
publicint value() {
return super.value() * 47;
}
}; // Semicolon required
}
publicstaticvoid main(String[] args) {
Parcel7 p = new Parcel7();
Wrapping w = p.wrap(10);
}
} ///:~
Ugh, I left out the definition of class "Wrapping". Here it is below. By the way, the thing I'm really confused about is that, in example 1 (from my previous post), the book said that using a anonymous local inner class translated into code that implemented an interface. In this example, however, we're dealing with a class ("Wrapping"). I think that's a big piece of where my confusion is coming from.
publicclass Wrapping {
privateint i;
public Wrapping(int x) { i = x; }
publicint value() { return i; }
}