Problem with generic overriding...

I am trying to make a generic version of the Java Observer/Observable definitions, but have a problem getting it to compile.

The idea is to just provide a type-checked wrapper to the actual Java library.

The problem is on NotifyObservers, which gets the error:

Name clash: The method notifyObservers(A) of type ObservableD<S,O,A> has the same erasure as notifyObservers(Object) of type Observable but does not override it.

If I try to specify that it should override it (@Override), then it also complains:

The method NotifyObservers(A) of type Observable<S,O,A> must override a superclass method.

So, seems like it complains that it does match, and also doesn't.

?

* Generic version of Observer/Observable

*-- Derived

*

* The idea is thatthis version is usedfor type-checking by compiler,

* and then the library version actually gets loaded...

*

* From Wadler; ...

*/

//

interface ObserverD<Sextends ObservableD><S,O,A>,

Oextends ObserverD <S,O,A>,

A >{

publicvoid update ( S subject, A arg);

}

//

abstractclass ObservableD<Sextends ObservableD><S,O,A>,

Oextends ObserverD <S,O,A>,

A >extends java.util.Observable{

publicvoidaddObserver(O o){super.addObserver((java.util.Observer)o);};

protectedvoidclearChanged(){super.clearChanged();};

publicintcountObservers(){return super.countObservers();};

publicvoiddeleteObserver(O o){super.deleteObserver((java.util.Observer)o);};

publicbooleanhasChanged(){return super.hasChanged();};

publicvoidnotifyObservers(){super.notifyObservers();};

// @Override

publicvoidnotifyObservers(A a){super.notifyObservers((Object)a);};

protectedvoidsetChanged(){super.setChanged();};

}

[3520 byte] By [guthriea] at [2007-11-26 20:00:52]
# 1
This is due to erasure. The compiler sees the difference, as it sees generics, therefore the methods are distinct. At runtime, the generics get erased. For an unbound generic it will be erased to Object, given your own method the same signature as the inherited one, which will not work.
stefan.schulza at 2007-7-9 22:58:50 > top of Java-index,Core,Core APIs...
# 2

Yes, I see this, but what is the solution?

It seems to me that the compiler and run-time shoudl have a consensus on it; either it is considered the same since it will erase to that, and so I can wrapper it via overriding; or the converse, it does not, so is considered a distinct signature and I can add it and then call-through.

The former seems correct to me. My generic signature is is a more specific but compatible typed version of the erased form, so should be allowed to override it. The call-through is specifically scoped to super, so no problem there.

You say that "... giving your own method the same signature as the inherited one, which will not work" - but why not, then it would just be an overriden method. The semantics seem perfectly valid to me, and the logic that the copiler thinks of it one was as the same signature,and another way as different, so won't allow either, seems ... wrong!

guthriea at 2007-7-9 22:58:50 > top of Java-index,Core,Core APIs...
# 3

Well, I am not sure, if there is a solution. I just wanted to give you an answer to why the compiler complains. Obviously the signatures are not identical/compatible before erasure, so the one cannot override the other from the compiler's point of view. Maybe you should file this as bug, if it isn't already.

stefan.schulza at 2007-7-9 22:58:50 > top of Java-index,Core,Core APIs...
# 4
If you change public voidnotifyObservers(A a){super.notifyObservers((Object)a);}; to public voidnotifyObservers(O a){super.notifyObservers((Object)a);};Comilation error is removed.
gssa at 2007-7-9 22:58:50 > top of Java-index,Core,Core APIs...
# 5

> If you change

> public voidnotifyObservers(A

> a){super.notifyObservers((Object)a);};

> to

> public voidnotifyObservers(O

> a){super.notifyObservers((Object)a);};

>

> Comilation error is removed.

Great idea. Replacing code with wrong code does not help solving the problem. The Observable is not going to notify about Observers but about a changed value.

stefan.schulza at 2007-7-9 22:58:50 > top of Java-index,Core,Core APIs...