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 4123706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:38:44+00:00 2026-05-20T23:38:44+00:00

I ran into this today and the only thing I can think is that

  • 0

I ran into this today and the only thing I can think is that this is a bug in the Java compiler. The following code compiles, but certainly seems incorrect (since testMethod has a differenet signature in the child but overrides the parent) and will throw class cast exceptions at runtime.

public interface TestInterface<T> {
  public List<String> testMethod(); // <-- List<String>
}
public class TestClass implements TestInterface {
  @Override
  public List<Integer> testMethod() { // <-- List<Integer> overriding List<String>!!
      return Collections.singletonList(1);
  }
}

And using the above structure:

public void test() {
  TestInterface<Boolean> test = new TestClass();
  List<String> strings = test.testMethod();
  for (String s : strings) {
    System.out.println(s);
  }
}

All of this compiles fine, but will obviously throw class cast exceptions if you run it.

If you remove <T> from TestInterface, or fill T in in the line TestClass implements TestInterface<T> then the code will no longer compile, which makes sense. Imo <T> should have no bearing on the compilation of testMethod since it plays no part in that method.
Maybe adding <T> to TestInterface is causing the compiler to type-erase the method signatures even though T doesn’t participate in those methods…?

Does anyone know what is going on here?

  • 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-20T23:38:45+00:00Added an answer on May 20, 2026 at 11:38 pm

    If you instantiate a generic class as a raw type, all generic type parameters contained in it are going to be omitted by the compiler, hence it gives you no warnings/errors during compilation. I.e. declaring

    public class TestClass implements TestInterface ...
    

    effectively degrades the code into

    public interface TestInterface {
      public List testMethod();
    }
    public class TestClass implements TestInterface {
      @Override
      public List testMethod() {
          return Collections.singletonList(1);
      }
    }
    

    which indeed compiles fine.

    A similar problem was posted a couple of weeks ago, the answer to which stating that it is not a compiler bug, rather a deliberate design decision for backward compatibility.

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

Sidebar

Related Questions

I ran into a bad chunk of code today that equates to this: [[asdf]].each
Today, I ran into some code that goes like this. namespace Foo { public
I ran into a interesting issue today. Check out this pseudo-code: void Loop() {
I ran into this today and have no idea why the C# compiler isn't
I ran into a little problem today that I can't seem to solve in
today I ran into this problem which really bugs me, as almost the code
I ran into an interesting iOS problem today involving a CATiledLayer. This only happend
I ran into a weird situation today doing some one-time sql code. This nested
I ran into this code today: $(#features).tabs(#someid > .someclass, { effect: 'fade', fadeInSpeed: 500,
OK, I ran into this today, when the TI TMS470 C++ compiler refused to

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.