File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Cannot instantiate the type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cannot instantiate the type?" Watch "Cannot instantiate the type?" New topic
Author

Cannot instantiate the type?

Rachel Barnes
Greenhorn

Joined: Oct 28, 2012
Posts: 11
I've got the program with no apparent errors, but am unable to run it due to three little problems.... On lines 25, 27, and 29 I am getting an error message that says "Cannot instantiate the type Health/Life/AutoInsurance" (each to their respective ones). Everything else is showing up as fine, so I'm not sure why this error is here, nor how to fix it. Is there something I've missed when trying to connect everything all together, or is there something else wrong? It's not offering any suggestions on how to fix it, so I'm stuck right at this final part.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You can't create instances of abstract classes or interfaces. You can use them as reference types or return types, but the actual value must be the instance of a non-abstract class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Rachel Barnes wrote:I've got the program with no apparent errors...

Rob's basically covered your main problem, but there are several redundancies in your classes which may eventually come to bite you.

1. If you have a Monthly premium, you don't need an Annual one; and personally, I'd store the latter. This isn't just nitpicking; in the real world there's a distinct possibility that such a construct is illegal, since it could theoretically be used to implement a "penny-shaving" scam.

2. If your superclass already has an AnnualPremium, you don't need it in your subclasses.

Also: floating-point types are generally not what you want for values involving money; you're usually better off using BigDecimal. For more information, read this or (a slightly simpler version) this.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rachel Barnes
Greenhorn

Joined: Oct 28, 2012
Posts: 11
Rob Spoor wrote:You can't create instances of abstract classes or interfaces. You can use them as reference types or return types, but the actual value must be the instance of a non-abstract class.



Took care of the 'abstract' issue and it's running now. However, I'm not receiving the right output. The policy number and monthly premium are showing up as zero. The annual is fine. Can you help me locate where my problem is?

And for the other commenter, the Annual bit is required for the assignment for whatever reason, so I have to keep that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Lines 53 and 54 have it the wrong way around - you're assigning the field value (still 0) to the parameter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot instantiate the type?