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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:44:48+00:00 2026-05-30T06:44:48+00:00

If I have an abstract class like this: public abstract class Item { private

  • 0

If I have an abstract class like this:

public abstract class Item
{
    private Integer value;
    public Item()
    {
        value=new Integer(0);
    }
    public Item(Integer value)
    {
        this.value=new Integer();
    }
}

And some classes deriving from Item like this:

public class Pencil extends Item
{
    public Pencil()
    {
        super();
    }
    public Pencil(Integer value)
    {
        super(value);
    }
}

I have not understood why I can’t call the constructor using a generic:

public class Box <T extends Item>
{
    T item;
    public Box()
    {
        item=new T(); // here I get the error
    }
}

I know that is possible to have a type which hasn’t a constructor, but this case is impossible because Pencil has the constructor without parameters, and Item is abstract.
But I get this error from eclipse:
cannot instanciate the type T
I don’t understand why, and how to avoid this?

  • 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-30T06:44:49+00:00Added an answer on May 30, 2026 at 6:44 am

    There is no way to use the Java type system to enforce that a class hierarchy has a uniform signature for the constructors of its subclasses.

    Consider:

    public class ColorPencil extends Pencil
    {
        private Color color;
    
        public ColorPencil(Color color)
        {
            super();
            this.color=color;
        }   
    }
    

    This makes ColorPencil a valid T (it extends Item). However, there is no no-arg constructor for this type. Hence, T() is nonsensical.

    To do what you want, you need to use reflection. You can’t benefit from compile-time error checking.

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

Sidebar

Related Questions

Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Say I have classes declared like this: public abstract class IdentifiableEntity { public boolean
I have a model that looks something like this: public abstract class Parent {
I have an abstract class BaseItem declared like this: public abstract class BaseItem {
I have the following classes : public abstract class Item { //... } public
I have a set of classes something like this: abstract class CollectionAbs implements Iterator
I have an abstract entity base class defined like this: public abstract class SessionItem
I have a class hierarchy like this public abstract class CalendarEventBase{} public class TrainingEvent
I have a situation like this: public abstract class BaseClass { public abstract string
I have a parent class, like this: public abstract class Business<T> : IBusiness<T> where

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.