Java - "YList is abstract; cannot be instantiated"

Hi, I've a Java abtract class "YList", and in many methods I need to return an instance of YList, Java doesn't allow using "new YList()", can anyone tell me how to achieve this?

The program's like this:
public abstract class YList<E>() {
    public YList<E> trim() {
        result = new YList<E>(); // Not allowed by Java


        return result;

trim() is just one of the many methods where it's needed.

Thanks in advance!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can't make an instance of an abstract class

Abstract classes (by definition) need to be extended as they have abstract methods that must be overriden

If your class has no abstract methods, then you should be able to get rid of the "abstract" bit in the class definition line like:

public class YList<E>() {
uxlinAuthor Commented:
Thank you, yeah I learned the hard way that I can't make an instance of an abstract class.

Is there any other way that the method can be written?

I know for trim() I can use a abstract duplicate() method, but there are lots of other methods where duplicate() would be inefficient.
You could have the trim() method actually act on the current instance...

  public void trim() {

if you want to return the changed list (for chaining purposes) you could do:

  public YList<E> trim() {
        return this ;

It would then be up to the user to make a copy if they want to keep an untrimmed version of the list...

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

uxlinAuthor Commented:
Thanks you for your comment, but trim() is like the trim() in String, cannot change the current instance. If it can change the current instance, of course thing are much easier.
uxlinAuthor Commented:
*Thanks you for your comment = Thank you for your comments
trim would have to be defined in the concrete class then, not in the abstraction...

uxlinAuthor Commented:
It, and many other similar methods need to be defined in the abstract class.

I'm wondering if there's anything like:
new this.type();

'cause appearantly we can't use:
new this();
you might be able to do

    YList<E> result = (YList<E>)this.clone() ;

To make a copy of the current list...
Since I'm not sure what you are trying to achieve, I'm not going to comment on the way you're approaching it. Instead, here is a way to achieve what you are doing using reflection (tested on Java 1.4.2):

public abstract class AbstractClass {
      public AbstractClass getInstance() {
            try {
                  return (AbstractClass) this.getClass().getConstructor(new Class[0]).newInstance(new Object[0]);
            } catch (Exception e) {
                  return null;

Now when you declare a concrete class that inherits AbstractClass and call the getInstance method, you will get an instance of the concrete class.

This is a simplified example, I'm leaving the actual details to you. You should also pay attention to the constructors of your concrete class -- if there is no constructor matching the parameters passed to getConstructor (and the subsequent invocation of the constructor), you will get an exception.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
uxlinAuthor Commented:
Thank you so much!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.