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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:51:31+00:00 2026-05-15T12:51:31+00:00

Within my code a have the following abstract superclass public abstract class AbstractClass<Type extends

  • 0

Within my code a have the following abstract superclass

public abstract class AbstractClass<Type extends A> {...}

and some child classes like

public class ChildClassA extends AbstractClass<GenericTypeA> {...}

public class ChildClassB extends AbstractClass<GenericTypeB> {...}

I’m searching for an elegant way how I can use the generic type of the child classes (GenericTypeA, GenericTypeB, …) inside the abstract class in a generic way.

To solve this problem I currently defined the method

protected abstract Class<Type> getGenericTypeClass();

in my abstract class and implemented the method

@Override
protected Class<GenericType> getGenericTypeClass() {
    return GenericType.class;
}

in every child class.

Is it possible to get the generic type of the child classes in my abstract class without implementing this helper method?

BR,

Markus

  • 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-15T12:51:32+00:00Added an answer on May 15, 2026 at 12:51 pm

    I think its possible. I saw this was being used in the DAO patterns along with generics. e.g.
    Consider classes:

    public class A {}
    public class B extends A {}
    

    And your generic class:

      import java.lang.reflect.ParameterizedType;
      public abstract class Test<T extends A> {
    
         private Class<T> theType;
    
         public Test()  {
            theType = (Class<T>) (
                   (ParameterizedType) getClass().getGenericSuperclass())
                  .getActualTypeArguments()[0];
         }
    
         // this method will always return the type that extends class "A"
         public Class<T> getTheType()   {
            return theType;
         }
    
         public void printType() {
            Class<T> clazz = getTheType();
            System.out.println(clazz);
         }
      }
    

    You can have a class Test1 that extends Test with class B (it extends A)

      public class Test1 extends Test<B>  {
    
         public static void main(String[] args) {
            Test1 t = new Test1();
    
            Class<B> clazz = t.getTheType();
    
            System.out.println(clazz); // will print 'class B'
            System.out.println(printType()); // will print 'class B'
         }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure: abstract class Parent {} class Child : Parent {
I have the following code within the class. basically what i need is it
I'd like to execute JavaScript code from within a C# assembly and have the
I'm using the following code within the JCProperty class to retrieve data from a
I have a custom XML file format which can contain blocks of code within
I have a block of code that is repeated within a DB2 stored procedure.
I'm using the following code within a VB 6.0 application to allow give the
I would like to call a windows program within my code with parameters determined
I'm trying to use the EntLib 3.1 within .net code for a dll which
Here's a snippet of code from within TurboGears 1.0.6: [dispatch.generic(MultiorderGenericFunction)] def run_with_transaction(func, *args, **kw):

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.