• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Constructor inheritance issues  RSS feed

 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement a program that has one parent class RoundShape with 2 children Sphere and Cone. I used extends from RoundShape but now I am having trouble with my constructors in the children. It gives me an error that the constructor cannot be applied to that given type. So I researched a bit and found some code using a super constructor but that won't work for me either.  I feel like I am doing exactly what the code tutorial is doing but I can't seem to get it to work. It is probably something simple but I can't seem to figure it out. I have copied my code below and I have also attached the tutorial. Any help would be appreciated.


inheritance-tut.PNG
[Thumbnail for inheritance-tut.PNG]
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error that the constructor cannot be applied to that given type. 

Please copy the exact text of the error message and paste it here.
 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so it is an IDE hint not necessarily and error. It actually won't compile because I cannot construct any new objects(and I have already referenced them in my main). So It won't stay up for me to screenshot but this is what it says verbatim:
   
      constructor RoundShape in class RoundShape cannot be applied to given types;
         required: double
         found: no arguments
         reason: actual and formal argument lists differ in length

  



 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructor RoundShape in class RoundShape cannot be applied to given types;
required: double
found: no arguments 

The compiler did not find a RoundShape constructor with no args.  It did find a constructor with a double arg.  Change the code so it passes the required double to the constructor.
 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help. This is what I did to correct it. There were also several other run time errors in my code. I have attached my corrected code just in case another newbie like me stumbles across this question.

Some of my errors were: 1) didn't need to provide  setVolume and setArea. They needed to be included in getArea and getVolume instead. They also didn't need to be in my constructor because my toString calls the method. 3) I used the super() constructor to take care of my inheritance issue.  There were probably quite a few more tweaks but those are the ones that stuck out. My new code is below. I am sure it is far from the best and most efficient way to do it but it seems to work.


 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

I would stick to radius or radius and height as fields; the more fields you have the more there is to go wrong. Change all the int literals to double literals. Not 3 but 3.0 (or 3d).You can alter the order of factors since the multiplication operator is associative and commutative.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got it working.
Note: Lines 63 and 87 are redundant.  Those assignments are done in the super constructor.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!