• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1118
  • Last Modified:

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

        result.trimThis();
        result.trimThat();

        return result;
    }
}

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

Thanks in advance!
0
uxlin
Asked:
uxlin
  • 5
  • 4
1 Solution
 
TimYatesCommented:
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>() {
 
0
 
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.
0
 
TimYatesCommented:
You could have the trim() method actually act on the current instance...

  public void trim() {
        this.trimThis();
        this.trimThat();
    }

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

  public YList<E> trim() {
        this.trimThis();
        this.trimThat();
        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...

Tim
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.
0
 
uxlinAuthor Commented:
*Thanks you for your comment = Thank you for your comments
0
 
TimYatesCommented:
trim would have to be defined in the concrete class then, not in the abstraction...

Tim
0
 
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();
0
 
TimYatesCommented:
you might be able to do

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

To make a copy of the current list...
0
 
RytmisCommented:
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.
0
 
uxlinAuthor Commented:
Thank you so much!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now