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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:12:09+00:00 2026-05-14T19:12:09+00:00

I want to have a function defined in a superclass that returns an instance

  • 0

I want to have a function defined in a superclass that returns an instance of the subclass that is used to invoke the function. That is, say I have class A with a function plugh. Then I create subclasses B and C that extend A. I want B.plugh to return a B and C.plugh to return a C. Yes, they could return an A, but then the caller would have to either cast it to the right subtype, which is a pain when used a lot, or declare the receiving variable to be of the supertype, which loses type safety.

So I was trying to do this with generics, writing something like this:

class A<T extends A>
{
  private T foo;
  public T getFoo()
  {
    return foo;
  }
}
class B extends A<B>
{
  public void calcFoo()
  {
    foo=... whatever ...
  } 
}
class C extends A<C>
{
   public void calcFoo()
  {
    foo=... whatever ...
  } 
}

This appears to work but it looks pretty ugly.

For one thing, I get warnings on class A<T extends A>. The compiler says that A is generic and I should specify the type. I guess it wants me to say class A<T extends A<x>. But what would I put in for x? I think I could get stuck in an infinite loop here.

It seems weird to write class B extends A<B>, but this causes no complaints, so maybe that’s just fine.

Is this the right way to do it? Is there a better way?

  • 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-14T19:12:09+00:00Added an answer on May 14, 2026 at 7:12 pm
    class A<T extends A<T>> {
    

    Or, I prefer:

    class A<THIS extends A<THIS>> {
    

    Like java.lang.Enum. It uglifies client code that just wants A, but has to write A<?>.

    Note this does not in any way break LSP, and is GOOD OO.

    Or you could just use covariant return types.

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

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are using field access strategy (determined by @Id annotation).… May 16, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer Will I need a recursive function to iterate through a… May 16, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer You can still accumulate a list of callbacks, but you… May 16, 2026 at 11:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a function defined in AS3 that's gonna be called from client side
I have a group of objects which I am creating a class for that
I have a superclass which is defined in terms of a few internal types
I have function defined this way: def f1 (a, b, c = None, d
is is possible in Excel to have some sort of fixed function defined for
I have an R question--I want to make a vector of functions, and then
I have an IEnumerable of an item class defined like this: public class Item
Here's the deal. I have a big class hierarchy and I have this one
I have been able to create php function to determine a list of zip
I have multiple objects in a hierarchy which have common properties and methods inherited

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.