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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:12:31+00:00 2026-06-18T08:12:31+00:00

I have an Object called CAR declared as follows: public class Car{ public int

  • 0

I have an Object called CAR declared as follows:

public class Car{
   public int NumberOfWheels;
}  

and using another class I can retrieve three of its subclasses.
As follows:

public TruckA getCarA(int param){
   return new TruckA () {NumberOfWheels = param} 
}
public TruckB getCarB(int param){
   return new TruckB () {NumberOfWheels = param} 
}
public TruckC getCarC(int param){
   return new TruckC () {NumberOfWheels = param} 
}

How do I write in using generics ?
(I know it is too simple, I need an example for a more complicated case)

I want to create a method that creates a new (T), enters the parameter and returns it.

something like

public TruckC getCarC(int param){
   return getTruck<TruckC>(param)
}

Only that :
1. TruckA/B/C dont have constructor.
2. Car has no constructor. initialized using {}.
3. The method should receive only classes that derive from Car so that it can use its initialization ( {} ).

(something equivalent to ?? extends ? in java generics)
Possible ?

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-06-18T08:12:32+00:00Added an answer on June 18, 2026 at 8:12 am
    private static T GetTruck<T>(int param) where T : Car, new()
    {
        return new T() {NumberOfWheels = param} ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a list of objects called Car: public class Car { public
I have an object called Market in my C# 4 application: public class Market
I have an object class called BOM. It has a method, getChildItem() which returns
In Python, can an object have another object as an attribute? For example, can
I have a Car class. It's got a property called enginePower. On the other
I have an object called Reservations . Inside Reservations , there is a another
I have an object called user . I can get its name by user.name
I have an object called Person . it has several attributes in it; int
I have a class called class Car , which has be instantiated as the
I have an object called Person that has the following properties: int id; Name

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.