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!

Some generic anonymous class overriding methods compile, while others don't

843793Dec 8 2005 — edited Dec 10 2005
I have the following (stripped-down) code. The abstract class SessionHandler<T> is used by other code to define and run an operation that needs a session to something. This is best done in my code with anonymous classes, because of the shear number of operations defined. In the EntityOps<T> class, these work great. But, in the last class shown here, SomeClass, the anonymous class definition fails, though the semantics are almost identical. (List<T> vs.List<AnotherClass>) What am I doing wrong here? Or is this a bug in Java?

Thanks, Tom
public interface IEntityOps<T> {
    T get();
    List<t> getAll();
}

public abstract class SessionHandler<T> {
    abstract T handle(Session session) throws Throwable;
    public final T perform() {
        ... calls handle(session) ...
    }
}

// These anonymous class definitions compile fine!
public class EntityOps<T> implements IEntityOps<T> {
    public T get() {
        T ret = null;
        ret = new SessionHandler<T>() {
            T handle(Session s) throws Throwable {
                T ret = (some T object calculation);
                return ret;
            }
        }.perform();
        return ret;
    }
    public List<T> getAll() {
        T ret = null;
        return new SessionHandler<List<T>>() {
            List<T> handle(Session s) throws Throwable {
                List<T> ret = (some List<T> calculation);
                return ret;
            }
        }.perform();
    }
}

// This anonymous class definition fails with the error:
// "SomeClass.java": <anonymous someMethod> is not abstract and does not override abstract method handle()
//     in SessionHandler at line XX, column XX
public class SomeClass {
    public List<AnotherClass> someMethod() throws {
        List<AnotherClass> ret = null;
        ret = new SessionHandler<List<AnotherClass>>() {
            List<AnotherClass> handle(Session s) throws Throwable {
                List<AnotherClass> ret = (some List<AnotherClass> calculation);
                return ret;
            }
        }.perform();
        return ret;
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2006
Added on Dec 8 2005
11 comments
552 views