3

I'm getting a Compiler error when using generics in my project. I generate a sample code:

My Bean Interface

package sample;

public interface MyBeanInterface {
  Long getId();
  String getName();
}

My Bean Concrete Class

package sample;

public class MyBean implements MyBeanInterface {
    private Long id;
    private String name;

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

My Manager Interface

package sample;

import java.util.List;

public interface MyManagerInterface<T extends MyBeanInterface> {

    <EXCEPTION extends Exception> List<T> sortAll(List<T> array) throws EXCEPTION;

    List<T> sortAll2(List<T> array);

    <EXCEPTION extends Exception> List<T> sortAll3() throws EXCEPTION;

}

My Manager Concrete Class

package sample;

import java.io.IOException;
import java.util.List;

public class MyConcreteManager implements MyManagerInterface<MyBean> {

   @Override
   //this fails
   public List<MyBean> sortAll(List<MyBean> array) throws IOException {
       return null;
      }

    @Override
   //this works
    public List<MyBean> sortAll2(List<MyBean> array) {
        return null;
    }

    @Override
   //this works
    public List<MyBean> sortAll3() {
        return null;
    }

}

I tried using the method sortAll with no method parameters (sortAll()) in the interface and it compiles, using only the exception in the interface also works, but using both not.

Thanks.

1
  • It would be helpful if you include the compilation error message you got. Commented Apr 29, 2015 at 12:13

1 Answer 1

2

With regards to the sortAll(List<T> list) method, you have to do:

@Override
public <E extends Exception> List<T> sortAll(List<T> array) throws E {
    // TODO Auto-generated method stub
    return null;
}

and then, when invoking the method to explicitly set the methods type-parameter:

try {
    new MyConcreteManager().<IOException>sortAll(...);
} catch (IOException e) {

}

The sortAll3() implementation compiles fine, because in Java, when a method definition overrides another one, it is not allowed to throw additional checked exceptions, but it may throw fewer.

Sign up to request clarification or add additional context in comments.

5 Comments

Wow. I didn't know you could even do this with throws type-parameters, where they're parameterized at the method level instead of the class level. Is there a reason to do so, instead of just saying throws Exception and letting subclasses narrow the signature in the old pre-generics manner? How do you get it to work if one of the method implementations might actually throw an exception? I put a throw new IOException(); in to the sortAll body and it gave me a compiler error about undeclared exceptions. (Sorry; I couldn't easily find doco on this variant of generics.)
when you do throw new IOException() then your method must have a throws clause. However, if you've decided to narrow the list of possibly thrown checked exceptions, then there's no reason do have such throw new IOException() statements, right? :)
I'm still confused. With the interface as defined here, including the signature: <EXCEPTION extends Exception> List<T> sortAll(List<T> array) throws EXCEPTION; – what does that throws clause actually mean? What exceptions are implementing classes allowed to throw from that method? If I change the throws in the overridden method to be throws IOException instead of <E extends Exception> ... throws E, then the compiler complains that I haven't overridden the abstract inherited method.
I guess I'm asking, what does one do in a subclass to allow it to actually throw an IOException from its overridden sortAll method?
To be more specific: here's how I was thinking one might want to use the interface. What would one do to make this compile? Or am I misunderstanding what the parameterized throws list means? gist.githubusercontent.com/apjanke/a185d27b4217d893d3e7/raw/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.