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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:22:53+00:00 2026-06-05T02:22:53+00:00

So I am attempting to create a class which uses a generic which extends

  • 0

So I am attempting to create a class which uses a generic which extends an abstract class.
Ex.

public abstract class Template<C extends Abstract>

However, I need to instantiate this ‘C’ within my class. I figured I might be able to do this:

C s = (C) ((C)new Object()).getClass().getConstructor(Known.class,AnotherKnown.class).newInstance(objectOfKnownType,objectOfAnotherKnownType);

So my question is basically whether this is possible. I feel like the

((C) new Object()).getClass()

might give me some problems.

What if i changed it to:

C a;
C s = (C) (a.getClass().getConstructor( ... ).newInstance( ... ));
  • 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-06-05T02:22:55+00:00Added an answer on June 5, 2026 at 2:22 am
    (C) new Object()
    

    is not valid and may give you a ClassCastException later, since an Object is not a C.

    The way generics are implemented means Template will have no knowledge of the C it was instantiated with, you will have to create a method or constructor that takes a Class object.

    public abstract class Template<C extends Abstract>
    {
        private Class<C> classOfC;
    
        public Template( Class<C> clazz ) {
            classOfC = clazz;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where
I am attempting to create a template AutoClass that create an arbitrary class with
I am attempting to create a class in vb.net from the below JSON example.
I'm attempting to use Google's Guava ImmutableSet class to create a set of immutable
Im attempting to create a new operator :? on lists, which operates the same
I attempting to create custom tabs using this . But when I try to
I'm attempting to create thumbnails out of some images, each of which isn't necessarily
I'm attempting to create a class with many parameters, using a Builder pattern rather
I am attempting to create a Backbone.js view based on a Twitter bootstrap-modal, which
I am attempting following code to create multiple instances of a class at run-time

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.