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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:08:33+00:00 2026-05-28T04:08:33+00:00

I have an abstract class Abstr<T> and an extending class Ext<T> . I need

  • 0

I have an abstract class Abstr<T> and an extending class Ext<T>. I need Abstr to know, with which T its extension was initialized so it can return that type. So far I’ve done this by giving the type to the constructor and then calling the superconstructor to save it. But this should work with reflections too I think. So far like I said:

public abstract class Abstr<T> {

    private Class<T> type;

    public Abstr(Class<T> _class) {
        this.type = _class;
    }

}

public class Ext<T> extends Abstr<T> {

    public Ext(Class<T> _class) {
        super(_class);
    }

}

public void someMethod() {
    Ext<String> ext = new Ext<String>(String.class);
}

Using reflections should rid me of calling any constructor as the extending default constructor will call the abstract classes default constructor which should then save the type of the calling constructor:

public Abstr() {    
    this.type = (Class<D>) this.getClass().getTheTypeParameter(); // something along these lines
}

Thanks!

  • 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-28T04:08:34+00:00Added an answer on May 28, 2026 at 4:08 am

    If you really need a method in Abstr similar to

    public Class<T> getType()
    

    you have no choice but to pass the Class isntance in the constructor (or in any other method). You cannot retrieve the Class of T due to type erasure, which basically comes down to the fact that T is not available at runtime, only at compile time.

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

Sidebar

Related Questions

I have an abstract class called camera which PointCamera uses as its super class.
I have an abstract class. I'm extending that class. I'm getting this error: Fatal
I have an abstract class (I know that it will not compile this way,
I have an abstract class that has a shared method which creates a new
I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java .
I have an abstract class, Foo, that has a non-abstract method called Bar. I
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have an abstract class that has a virtual method. The method is virtual
Suppose I have abstract class A and abstract class B which inherits from A,
I have an Abstract class called Function which has 2 inheriting classes (actually 6,

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.