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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:59:09+00:00 2026-05-18T03:59:09+00:00

I have a system of object instances that contain a reference to a definition

  • 0

I have a system of object instances that contain a reference to a definition object. I have a top-level class for each inheritance tree. The instance object has a generic reference to the corresponding definition class.

Using generics in the getter, a subclass of the top-level object can get the right type of definition without casting. However, an abstract subclass that is subclassed again cannot:

class Def { }

abstract class Animal<D extends Def> {
    D def;
    D getDef() { return def; }
}

class CatDef extends Def { }
class Cat extends Animal<CatDef> { }

abstract class BearDef extends Def { }
abstract class Bear<D extends BearDef> extends Animal<D> { }

class BlackBearDef extends BearDef { }
class BlackBear extends Bear<BlackBearDef> { }

class AnimalDefTest {
    public static void main (String... args) {
        Cat cat = new Cat();
        CatDef catDef = cat.getDef(); // CatDef works fine

        Bear bear = new BlackBear();
        BearDef bearDef = bear.getDef(); // Error: Expected Def not BearDef? Why???
        BearDef bearDef2 = ((Animal<BearDef>)bear).getDef(); // Works
    }
}

Why does getDef require a Bear to be cast to (Animal<BearDef>) to get a BearDef? Bear is conclusively defined as extends Animal<? extends BearDef>.

[Edit] Even stranger, if I change the Bear class line:

abstract class Bear<D extends BearDef> extends Animal<BearDef> { }

(In which case D is unused and is irrelevant) it still doesn’t work. Erase D and the following line resolves the error in the code above (but doesn’t help me do what I need to do with subclass definitions):

abstract class Bear extends Animal<BearDef> { }
  • 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-18T03:59:09+00:00Added an answer on May 18, 2026 at 3:59 am

    You’re using the raw type Bear when in fact it should be parameterized with a type of BearDef. If you wrote

    Bear<BlackBearDef> bear = new BlackBear();
    

    or

    Bear<?> bear = new BlackBear();
    

    it’d work fine.

    Another thing: I’m not sure how you intend to use this, but it seems likely to me that you would be fine just doing this instead:

    abstract class Bear extends Animal<BearDef> {}
    
    class BlackBear extends Bear {
      // make use of covariant return type to make BlackBear return correct def
      @Override
      BlackBearDef getDef() { ... }
    }
    

    My reason for thinking this is that if you want a Bear whose getDef() method returns a BlackBearDef, that Bear reference would need to be parameterized as Bear<BlackBearDef>. In that case (for your example anyway) you effectively know that it’s a BlackBear from the declared type, so you might as well just be referring to it as a BlackBear. That said, it’s obviously not always so simple and your actual situation may not allow this.

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

Sidebar

Related Questions

No related questions found

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.