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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Quick java question on classes..(Exam Question)

  • 14-05-2009 11:30am
    #1
    Registered Users, Registered Users 2 Posts: 2,237 ✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 3,485 ✭✭✭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, Registered Users 2 Posts: 2,237 ✭✭✭techguy


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

    Thanks.


  • Registered Users, Registered Users 2 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