Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot instantiate the type?

 
Rachel Barnes
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rachel Barnes
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 53 and 54 have it the wrong way around - you're assigning the field value (still 0) to the parameter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic