Results 1 to 8 of 8
- 10-28-2011, 04:20 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 55
- Rep Power
- 0
Why do I need to call my constructor to get this to work?
Experimenting with polymorphism, made a simple program with a coin super class, each alternative value coin extends coin ect....no suprises or anything crazy, but when i run it as of now the console spits out "This coin is a null Its value is 0" no matter what item in the array i set it to print UNLESS , before the print line I insert "leftPocket[0].coin();" for each item in the array . It works like that just fine , but from what I know so far this doesnt make much sense, shouldnt my constructor construct without me manually calling it for each item?
Java Code:public class coin{ int value; String name; public void coin(){ value=0; name="Canadian Penny"; } public String toString() { return String.format("This coin is a %s\n Its value is:%s", name, value); } }Java Code:class pocket{ public static void main(String[] args){ coin leftPocket[]= new coin[5]; leftPocket[0]= new dime(); leftPocket[1]= new nickel(); leftPocket[2]= new quarter(); leftPocket[3]= new coin(); leftPocket[4]= new penny(); System.out.println(leftPocket[3].toString()); } }
- 10-28-2011, 04:32 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Why do I need to call my constructor to get this to work?
Constructors don't have a return type. It's hard to give an exact answer without seeing some of the code from the subclasses, but I'm assuming it has to do with your lack of a constructor (besides of course, the default one) in the coin class. Remove the 'void'
Also, use correct java naming conventions, all classes should have the first letter of each word capitalized. Coin, Penny, Nickel, Dime, Quarter, etc..
- 10-28-2011, 04:36 AM #3
Re: Why do I need to call my constructor to get this to work?
Do dime, nickel etc have their own constructors? If no then they do not initialise the value and name variables so they remain 0 and null. You need to add constructors to your subclasses. Either similar to the coin constructor or use a constructor with parameters so you can pass in values when you construct an object. Also, class names should begin with an uppercase letter.
- 10-28-2011, 04:37 AM #4
Re: Why do I need to call my constructor to get this to work?
Ooops!
Well spotted sunde. Apart from that everything else I said stands.Either similar to the coin constructor
- 10-28-2011, 04:58 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Why do I need to call my constructor to get this to work?
Ya, it almost slipped by me, but it seemed weird that he was calling leftPocket[3].coin()
- 10-28-2011, 07:02 AM #6
Member
- Join Date
- Dec 2009
- Posts
- 55
- Rep Power
- 0
Re: Why do I need to call my constructor to get this to work?
thats what my subclass looks like, taking the void off of the coin super class worked for the coin class , but im unsure what exactly you guys are telling me to do with the subclass. i think it would be less typing to just cut and paste my code and just change what ur talking about .Java Code:public class dime extends coin{ void coin(){ value=10; name="Dime"; } public String toString() { return String.format("This coin is a %s\n Its value is:%s",name,value); } }Last edited by sunde887; 10-28-2011 at 03:55 PM.
- 10-28-2011, 07:12 AM #7
Member
- Join Date
- Dec 2009
- Posts
- 55
- Rep Power
- 0
Re: Why do I need to call my constructor to get this to work?
i think i figured it out, i was doing a few things wrong ...the biggest problem being i was attempting to override the constructor of the coin class as if it was a reg method. ill still take any additional feedback you guys have to offer, obviously im still just learning all this stuff.
- 10-28-2011, 07:26 AM #8
Similar Threads
-
Unable to call classe constructor from main
By serdimay in forum New To JavaReplies: 13Last Post: 08-30-2011, 10:38 AM -
applet call dll work in Win2000 but not work in WinXP
By manhcuongtin4 in forum Java AppletsReplies: 1Last Post: 07-14-2011, 01:45 PM -
constructor won't work.
By nolsen01 in forum New To JavaReplies: 3Last Post: 09-13-2010, 05:55 PM -
Dll Call doesnt work
By INFACT in forum New To JavaReplies: 1Last Post: 10-04-2009, 09:31 PM -
Constructor call not calling
By Singing Boyo in forum New To JavaReplies: 5Last Post: 06-09-2009, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks