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

  • Home
  • SEARCH
  • 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 741259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:36:43+00:00 2026-05-14T08:36:43+00:00

I have a problem when compiling a generic class with an inner class. The

  • 0

I have a problem when compiling a generic class with an inner class. The class extends a generic class, the inner class also.

Here the interface implemented:

public interface IndexIterator<Element>
    extends Iterator<Element>
{
  ...
}

The generic super class:

public abstract class CompoundCollection<Element, Part extends Collection<Element>>
    implements Collection<Element>
{
  ...

  protected class CompoundIterator<Iter extends Iterator<Element>>
      implements Iterator<Element>
  {
    ...
  }
}

The generic subclass with the compiler error:

public class CompoundList<Element>
    extends CompoundCollection<Element, List<Element>>
    implements List<Element>
{
  ...

  private class CompoundIndexIterator
      extends CompoundIterator<IndexIterator<Element>>
      implements IndexIterator<Element>
  {
    ...
  }
}

The error is:

type parameter diergo.collect.IndexIterator<Element> is not within its bound
       extends CompoundIterator<IndexIterator<Element>>
                                             ^

What is wrong? The code compiles with eclipse, but not with java 5 compiler (I use ant with java 5 on a mac and eclipse 3.5). No, I cannot convert it to a static inner class.

  • 1 1 Answer
  • 1 View
  • 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-14T08:36:43+00:00Added an answer on May 14, 2026 at 8:36 am

    The Java Language Specification, §8.1.3, defines the semantics of subclassing inner types as follows:

    Furthermore, for every superclass S of
    C which is itself a direct inner class
    of a class SO, there is an instance of
    SO associated with i, known as the
    immediately enclosing instance of i
    with respect to S. The immediately
    enclosing instance of an object with
    respect to its class’ direct
    superclass, if any, is determined when
    the superclass constructor is invoked
    via an explicit constructor invocation
    statement.

    Note that the enclosing instance is only described to be of a particular class, not a particular type. As all instances of a generic type share the same class, the following code would be legal:

    class Base<E> {
        E e;
    
        protected class BaseInner<I extends E>{
            E e() { return e; }
        } 
    } 
    
    class StrangeSub extends Base<Integer> {
        protected class StrangeSubInner extends Base<String>.BaseInner<String> {}
    }
    

    Of course, this can be used to break the type invariant (i.e. cause heap pollution):

        StrangeSub ss = new StrangeSub();
        ss.e = 42;
        String s = ss.new StrangeSubInner().e();
    

    The eclipse compiler takes the Java Language Specification as face value, and accepts the above code without even emitting an “unchecked” warning. While arguably technically compliant with the JLS, this clearly violates its intent.

    The Sun Java Compiler rejects the declaration of StrangeSubInner with:

    Test.java:32: type parameter java.lang.String is not within its bound
            protected class StrangeSubInner extends Base<String>.BaseInner<String> {}
                                                                           ^
    

    Apparently the compiler didn’t simply check the type parameter against inner’s super class’ type parameter bound like eclipse did. In this case, I believe this the right thing to do, as the declaration is clearly unsafe. However, the Sun compiler equally rejects the following declaration, even though it is provably type safe:

    class StrangeSub extends Base<Integer> {
        protected class StrangeSubInner extends BaseInner<Integer> {}
    }
    

    My hunch is that verifying the consistency of this diamond-shaped type restrictions is beyond the capabilities of the Sun compiler, and such constructs are therefore summarily rejected instead.

    To work around this limitation, I’d first try to get rid of the type parameter to CompoundIterator.

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

Sidebar

Related Questions

I have a problem with the is operator comparing generic types. public interface ISomeInterface<T>
I have the following class using boost filesystem, but encountered the problem when compiling.
I have a problem with compiling an Oracle trigger via SQL*PLUS - I don't
I have a problem compiling with ocamlopt and floats I'm under Ubuntu 10.04 and
I have a problem compiling a program I made in BASIC. It's a DOS
Im doing authentication/authorization in a generic way but i have a problem, some behaviors
Hi I have a problem compiling my Qt app with Visual studio 2008 SP1.
I have a problem while compiling my code ( undefined reference to 'yylex' ).
I have problem with extremely long compiling time on my C++ code where I
I am new in C and I have problem with compiling this code. #include

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.