Search code examples
javamultiple-inheritance

Implement both Iterator and ResultSet interface in a single class - Conflict


I have a scenario where a single class needs to implement both Iterator and ResultSet interface.

Code looks like this

enter image description here

Error also is shown above.

Can you suggest if there is any alternative to this issue?


Solution

  • you are getting that error because both Resultset and Iterable have method next() in them, but have a different returns value

    resultset next has boolean as return value Iterator next has E as return value

    but the calling signature is the same. In java, you do not get polymorphism by changing only the return value; this has resulted in compiler error.

    instead of inheritance you can go for composition include Resultset within class as instance variable.

    public class MyClass<E> implements Iterator<E>, Iterable<E>{
        private ResultSet resultset;
    }