Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Quick java question on classes..(Exam Question)

  • 14-05-2009 12:30PM
    #1
    Registered Users, Registered Users 2 Posts: 2,238 ✭✭✭


    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,576 ✭✭✭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,238 ✭✭✭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