December 24, 2009

Inner classes

Inner classes are the same as nested classes
Method-local inner classes
Anonymous inner classes
Static inner classes (which are the same as static nested classes) are not like "regular" inner/nested classes; they're regular classes which are static members of their containing classes, similar to a static String or static int.

FINISH THIS

Overriding vs. Overloading vs. Hiding

  • Overloading is using the same method name with a different parameter list.
  • Overriding is replacing a superclass method with a different and/or more specific one in a subclass.
  • Hiding is similar to overriding but not quite the same. In the special case where a subclass's static method has the same signature as a parent class's static method, the subclass's method hides rather than overrides the parent class's method. This makes the parent class's method completely unaccessible. TODO: Find out whether this is the only situation in which hiding applies.

Useful links
The best explanation I've found so far, from the JavaRanch FAQ

Also useful, from the Java Tutorials

This StackOverflow post is technically about .NET, but I think it's still correct for Java

Some examples from the Java Language Specification

December 22, 2009

Parameterizing Wicket tables

Constructors for DefaultDataTables — including AjaxFallbackDefaultDataTables — include one or the other of:


IColumn<T>[] columns
java.util.List<IColumn<T>> columns


In other words, the parameter of the table — T — should be the same as the parameter of its columns.

Wicket ChoiceRenderers should take the same parameter as their DropDownChoices

From the Wicket 1.4.1 API:


new DropDownChoice("users", new Model(selectedUser), listOfUsers, new ChoiceRenderer("name", "id"));

Java generics, name clash and erasure

In progress: Adding to this post

General Overview

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

FILL IN fancy term

It is not legal to use List<SubtypeOfFoo> as a subtype of List<Foo>. This is counterintuitive, especially because it IS legal to treat SubtypeOfFoo[] as a subtype of Foo[]. (Aside: new Integer[] { 1, 2, 3 } instanceof Number[] returns true.) This makes sense, because it prevents the following error from occurring:

// Bicycle and Train are subclasses of Vehicle
public void foo() {
List<Bicycle> bikes = new ArrayList<Bicycle>
addVehicle(bikes);
}

public void addVehicle(List<Vehicle> vehicles) {
vehicles[0] = new Train();
}



The matching code using arrays, shown below, compiles okay but will throw an ArrayStoreException at runtime. This works for arrays but not for generified collections because type erasure (see next section) makes it impossible for the JVM to throw a generified-collection equivalent of the ArrayStoreException.


// Bicycle and Train are subclasses of Vehicle
public void foo() {
Biycle[] bikes = { new Bicycle(), new Bicycle(), new Bicycle() };
addVehicle(bikes);
}

public void addVehicle(Vehicle[] vehicles) {
vehicles[0] = new Train(); // Compiles, but will explode at runtime
}


ADD MORE INFO HERE

Type Erasure

This is basically a fancy term for "all information about generics goes away before runtime." To give slightly more detail, a modern compiler will check generics for correctness to the best of its ability and insert casts where needed. As soon as that's done, it discards — or, erases — the generics and finishes compilation. By runtime, the JVM has no idea that generics were ever used; the code looks the same as it would have before J2SE 5, when generics were introduced.

The name clash error

Remember parameters for parent classes! From http://elliottback.com/wp/name-clash-the-method-blah-has-the-same-erasure-as-type-blah-but-does-not-override-it/:

Name clash: The method [foo] of type [some generic type] has the same erasure as [foo again] of type [some other generic type] but does not override it

In some cases, this can be fixed by adding parameters to the superclass name in the class containing foo. That is, change

class Bar<T> extends SuperBar

to

class Bar<T> extends SuperBar<T>

Exceptions in Java

Exceptions in Java - flesh out this post

http://java.sun.com/docs/books/tutorial/essential/exceptions/advantages.html
http://www.artima.com/intv/handcuffs.html