Results 1 to 8 of 8
  1. #1
    fresh83 is offline Member
    Join Date
    Dec 2009
    Posts
    55
    Rep Power
    0

    Default 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());
        
      }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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..

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Why do I need to call my constructor to get this to work?

    Ooops!

    Either similar to the coin constructor
    Well spotted sunde. Apart from that everything else I said stands.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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()

  6. #6
    fresh83 is offline Member
    Join Date
    Dec 2009
    Posts
    55
    Rep Power
    0

    Default Re: Why do I need to call my constructor to get this to work?

    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);
     }
    }
    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 .
    Last edited by sunde887; 10-28-2011 at 04:55 PM.

  7. #7
    fresh83 is offline Member
    Join Date
    Dec 2009
    Posts
    55
    Rep Power
    0

    Default 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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Why do I need to call my constructor to get this to work?

    Constructors are not inherited so you cannot override them. What I was suggesting is that each subclass has its own constructor to set the value of the instance variables. eg the dime constructor would set value to 10, nickel to 5 etc.

Similar Threads

  1. Unable to call classe constructor from main
    By serdimay in forum New To Java
    Replies: 13
    Last Post: 08-30-2011, 11:38 AM
  2. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 02:45 PM
  3. constructor won't work.
    By nolsen01 in forum New To Java
    Replies: 3
    Last Post: 09-13-2010, 06:55 PM
  4. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 10:31 PM
  5. Constructor call not calling
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-09-2009, 02:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •