• Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot find symbol - constructor error while extending a class  RSS feed

 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying inheritance. Created a class Box with no default constructor. BoxWeight extends Box. BoxWeight has no default constructor defined. When I try Shipment extends BoxWeight, I get error"cannot find symbol - constructor BoxWeight()". Why I donot get this error when BoxWeight extends Box.





Request your assistance.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use four spaces for indenting. Use {} after every if, else, do, while, for and catch keyword. Always use one space before and one space after every binary operator (similarly with four spaces for the ternary operator).I think there was a pair of {} not correctly matched, and I have commented out your shipment class as incomplete. [Always use //, not /* */, for commenting out code.] Then it compiled and ran all right.

Get rid of that no-args constructor. You do not want to create a Box object with negative sizes. If a box always has a size, then its constructor must always take a size (ie l, h, w parameters). Unless you have some reason to use a default size, eg 10 × 10 × 10.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:... and I have commented out your shipment class as incomplete. ... Then it compiled and ran all right.

But it was the error in the shipment class that the OP was asking about.

You haven't put a constructor in your shipment class, therefore the compiler will implicitly add a no-args constructor. The only thing this constructor will do is make a call to it's parent no-arg constructor i.e. it will look like this

But the parent class (BoxWeight) doesn't have a no-args constructor (because you have added constructors with arguments, so the implicit no-arg constructor is not added) - hence the error.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joanne. I was mistaken there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!