Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

override a methode returning Object with a method returning generics

843793Apr 3 2009 — edited Apr 7 2009
I work on a project in java 1.5 where I have to use old library in java 1.4 (I cannot modify the code of this library)

I have the following class in this library :
class A {
    public Object getObject(String key) {
        // Some code to get the Object and return it
    }
}
I made another class in my projet, extending the class A. Because all the project is in java 1.5 and use heavilly the getObject method, thus leading to a lot of unchecked class cast warning, I wanted to override this methode to make it generics to concentrate all the warning in only one place :
class B extends A {
    @SuppressWarning("unchecked")
    public <T> T getObject(String key) {
        return (T)super.getObject(key);
    }
}
I also tried, just to be sure :
class B extends A {
    @SuppressWarning("unchecked")
    public <T extends Object> T getObject(String key) {
        return (T)super.getObject(key);
    }
}
I get this error, at compile time : Name clash: The method getObject(String) of type B has the same erasure as getObject(String) of type A but does not override it

Why does this cause problem ? The two method has the same erasure, and I'm pretty sure that T is a subtype of Object, so why doesn't the method of class B override the one of class A ?

I solved the problem for the moment by changing the name of the class B method to getObjectGen, but I would like to understand.

Thanks a lot !

Edited by: Lyrgard on Apr 3, 2009 1:08 AM

Edited by: Lyrgard on Apr 3, 2009 1:11 AM

Edited by: Lyrgard on Apr 3, 2009 1:13 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 5 2009
Added on Apr 3 2009
19 comments
496 views