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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:02:25+00:00 2026-05-26T17:02:25+00:00

If have a type hierarchy that starts out with a generic type class A<T>

  • 0

If have a type hierarchy that starts out with a generic type

class A<T> { ... }
class B extends A<Qux> { ... }
class C extends B { ...}

So far so good. Now I have a different class (also a generic) that uses A as parameter:

class Foo<T> extends Bar<T> {
  setA(A<T> a){ ...}  
}

Now I want to use all this:

Foo<Qux> foo = new Foo<Qux>();
foo.setA(new C());

The last line gives an error “setA in type Foo is not application for the arguments C”.

I can’t figure out what’s wrong. In my opinion it should work because C extends B which extends A, which is the the parameter type that should be used … thanks for any hints what is wrong with my assumptions.

Update: Using the code posted by Oli, I managed to get closer to the source of the error. It occurs once I replace B with my original class. So it must be located in the class that does away with the “generic” part. When trying to cast to the parent class Eclipse says it can’t. I’ll keep looking there, I know it should be working … the problem can be reduced to this:

C c = new C();
A<Qux> a = (A<Qux>)c;

In my code this results in the error

Cannot cast from C to A<Qux>

I still have no idea why this happens. Is there any reason why a child class cannot be cast to it’s parent class? There is no such reason. Because if you have such a problem it might just be your IDE. I restarted Eclipse for other reasons, checked again and it worked.

  • 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-26T17:02:25+00:00Added an answer on May 26, 2026 at 5:02 pm

    The following compiles fine for me:

    public class Test {
    
        static class Qux {};
    
        static class A<T> {}
        static class B extends A<Qux> {}
        static class C extends B {}
    
        static class Bar<T> {}
        static class Foo<T> extends Bar<T> {
            void setA(A<T> a) {}
        }
    
    
        public static void main(String[] args) {
            Foo<Qux> foo = new Foo<Qux>();
            foo.setA(new C());
    
            // From question update
            C c = new C();
            A<Qux> a = c;
        }
    }
    

    Your problem must lie elsewhere.

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

Sidebar

Related Questions

I have a class hierarchy, this one: type TMatrix = class protected //... public
A have a class hierarchy that looks somethign like this: class AbstractDataType { public:
I have a hierarchy of classes that all derive from a base type and
I have problem with Activities navigation, searching works good. Activities hierarchy looks like that:
I have this type of class hierarchy: public interface IA{} public interface IB{void Foo();}
My problem is HTML and CSS related. I have a hierarchy type structure that
I have a class hierarchy that I want to map across several tables using
I have your run of the mill nested set hierarchy type setup with the
I have a type and an interface and I need to verify that the
I have type hierarchy defined like this: interface IMyClass { } interface IBase1<T> {

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.