Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quick java question on classes..(Exam Question)

Options
  • 14-05-2009 12:30pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Hi,

    This is a question from a past exam paper but I can't figure out the answer.
    When the following classes are compiled, line 10 (indicated by a comment) generates a compile error. Why is this? Modify one of the classes so that it will compile without error. Hint: there are two ways to achieve this.
    
    class CD
    {
         String title;
         CD(String s)
         {
              title = s;
         }
    }
    
    class Music_CD extends CD
    {
         int num_tracks;
         Music_CD()
         {
               num_tracks = 0;
         }
    }
    

    I'm getting an error about a CD constructor in the CD class??

    I simply don't know the answer to this one :(


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Havn't done java in ages but it looks like your constructor for CD is private, hince it cannot be constructed. Or the the class should be defined as public class {


  • Registered Users Posts: 3,206 ✭✭✭techdiver


    Just create a default constructor in "class CD", like this:

    class CD
    {
    String title;
    CD(String s)
    {
    title = s;
    }
    CD()
    {
    }
    }
    class Music_CD extends CD
    {
    int num_tracks;
    Music_CD()
    {
    num_tracks = 0;
    }
    }


  • Closed Accounts Posts: 336 ✭✭geuro


    techguy wrote: »
    Hi,

    This is a question from a past exam paper but I can't figure out the answer.
    When the following classes are compiled, line 10 (indicated by a comment) generates a compile error. Why is this? Modify one of the classes so that it will compile without error. Hint: there are two ways to achieve this.
    
    class CD
    {
         String title;
         CD(String s)
         {
              title = s;
         }
    }
    
    class Music_CD extends CD
    {
         int num_tracks;
         Music_CD()
         {
               num_tracks = 0;
         }
    }
    

    I'm getting an error about a CD constructor in the CD class??

    I simply don't know the answer to this one :(

    You need a no-args constructor in class CD. eg

    CD(){}

    If a superclass doesnt contain a no-args constructor (as it doesnt in this case because you have supplied a constructor with arguments), well then you cannot have a no-args constructor in a subclass.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Thanks Guys..some good responses there, all cleared up now!

    Thanks.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Another way is to have:

    [PHP]
    class CD
    {
    String title;
    CD(String s)
    {
    title = s;
    }
    }

    class Music_CD extends CD
    {
    int num_tracks;
    Music_CD()
    {
    super("title");
    num_tracks = 0;
    }
    }
    [/PHP]


  • Advertisement
Advertisement