Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 341503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:42:54+00:00 2026-05-12T10:42:54+00:00

I have an interface for a variety of classes, all of which should implement

  • 0

I have an interface for a variety of classes, all of which should implement Iterator, so I have something like

public interface A extends Iterable<A> { ...otherMethods()... }

For the concrete classes, however, this means I must use

public class B implements A { public Iterator<A> iterator() {...} }

when I’d prefer (or at least, think I’d prefer) to use public Iterator<B> iterator() {...} so that concrete use of the class could have the explicit type (in case I wanted methods that weren’t in the interface to be available, or some such. Maybe that should never come up? Or it’s poor design if it does?

The flipside is that using the Iterator interface

public interface A extends Iterator<A> { ...otherMethods()... }

the concrete classes compile just fine with

public class B implements A { public B next() {...} }

What gives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:42:55+00:00Added an answer on May 12, 2026 at 10:42 am

    The concrete class compiles fine because B extends A, and since Java 5 a return type on a subclass can be a subclass of the return type on the superclass.

    As for the generic, I have hit the same wall, and you have two options that I know of.

    One is to paramaterize A:

     public interface A<T extends A> extends Iterable<T>
    

    then:

    public class B implements A<B>
    

    However, that has a disadvantage that you will need to give a parameter of A, instead of having it fixed, even if you want A:

    private class NoOneSees implements A<A>
    

    As to the question of if you actually want Iterator<B>, in the case of an Iterable, most likely that is preferable, and considering that these are interfaces, the need to redeclare the parameter is probably reasonable. It gets a little more complicated if you start inheriting concrete classes, and for things that have meaning other than an Iterable, where you may want covariance. For example:

    public interface Blah<T> {
        void blah(T param);
    }
    
    public class Super implements Blah<Super> {
        public void blah(Super param) {}
    }
    
    public class Sub extends Super {
        public void blah(Super param) {}
       //Here you have to go with Super because Super is not paramaterized
       //to allow a Sub here and still be overriding the method.
    }
    

    Similarly for covariance, you can’t declare a variable of type Iterator<A> and then assign an Iterator<B> in it, even if B extends A.

    The other option is live with the fact that further implementation/subclasses will still reference A.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have interface and implementing classes like public interface IA {} public class
I have an Interface like this: namespace QuickRoutes.Model.Utilities { public interface IRoutesManager { bool
I have an interface list which stores a variety of objects derived from that
I have interface defs like below. public interface IProvider { } public interface IProviderList
If I have interface IFoo, and have several classes that implement it, what is
I have interface Foo public interface Foo { public void test(); } Class FooChild
I have an interface: public interface: IA { ... } and I try to
I have next interface public interface IMyInterface { string this[string key] { get; set;
I have an application which I am developing using WPF\Prism\MVVM. All is going well
I have interface based on another: class IDrawable { public: virtual ~IDrawable(); }; class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.