Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: Calling constructor error

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy Calling constructor error

    Hi i'm trying to call super constructor but found the error :call to super must be first statement in constructor.

    This is my code in parent class (NO ERRORS)
    import java.util.Date;
    public abstract class GreetingCard implements Comparable {
    String message;
    Date arrivalDate;

    void greetingCard(String m, Date d){
    this.message = m;
    this.arrivalDate = d;
    }

    public int comPareTo(GreetingCard other){
    if(arrivalDate.equals(other.arrivalDate)){
    return 0;
    }
    else{
    if(arrivalDate.equals(other.arrivalDate)){
    return 1;
    }
    else{
    return -1;
    }
    }
    }

    abstract String formatMessage();
    }

    This is my code in extends class (ERRORS)
    import java.util.Date;
    public class BirthdayCard extends GreetingCard {
    String message;
    Date arrivalDate;

    void BirthdayCard(String bm, Date dd){
    super(bm,dd);
    this.message = bm;
    this.arrivalDate = dd;
    }

    String formatMessage(){
    return ("Happy Birthday" + this.message);
    }

    }

    Pls...pls help me...


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    The compiler is telling you that you're trying to call a super constructor that doesn't exist, and the compiler is (as usual) in fact correct. Remember that constructors must be named *exactly* the same as the class their in, both spelling and capitalization, and constructors return nothing, no void, no nothing.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    haliza hadi (February 15th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    so if i change my code in parent class to just greetingCard(String m, Date d){} i'm getting error now in my parent class. SO how should i write the code in?

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    Re-read my post and then look carefully at the line that contains the error and it will become apparent. Remember that the Java compiler is unforgiving.

  6. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    ok got it thanks...the compiler is unforgiving...

    --- Update ---

    ok now i have this error :BirthdayCard is not abstract and does not override abstract method compareTo(object) in comparable...

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    It's essentially the same problem. Again check spelling. Again check capitalization. This sort of thing should be automatic now when you receive these types of errors. You shouldn't need our help for similar issues.

  8. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    I'm still getting this same error messages. Sorry this is my first try doing object oriented programming
    I did re-check my spelling and caps everything seems to be fine to me. Cud u pls check for me?

  9. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    Please show your latest code attempt *with code tags* and in particular your compareTo method, the one that the compiler says is missing. And since that's the method the compiler complains about, again that's the method signature that you should check most carefully, again including capitalization.

  10. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    This is parent class
    import java.util.Date;
    public abstract class GreetingCard implements Comparable {
    String message;
    Date arrivalDate;

    GreetingCard(String message, Date arrivalDate){
    this.message = message;
    this.arrivalDate = arrivalDate;
    }

    public int comPareTo(GreetingCard other){
    if(arrivalDate.equals(other.arrivalDate)){
    return 0;
    }
    else{
    if(arrivalDate.equals(other.arrivalDate)){
    return 1;
    }
    else{
    return -1;
    }
    }
    }

    abstract String formatMessage();
    }

    import java.util.Date;
    public class BirthdayCard extends GreetingCard{ <---BirthdayCard is not abstract and does not override abstract method compareTo(Object) in Comparable
    ----
    (Alt-Enter shows hints)

    BirthdayCard(String bm, Date dd){
    super(bm,dd);
    this.message = bm;
    this.arrivalDate = dd;
    }
    public Date arrivalDate(){
    return arrivalDate;
    }
    String formatMessage(){
    return ("Happy Birthday" + this.message);
    }
    }

    import java.util.Date;
    public class Anniversary extends GreetingCard { <---Anniversary is not abstract and does not override abstract method compareTo(Object) in Comparable
    ----
    (Alt-Enter shows hints)
    String occassion;

    Anniversary(String am,Date ad){
    super(am,ad);
    this.message = am;
    this.arrivalDate = ad;
    }

    String formatMessage(){
    return("Happy "+occassion+"Anniversary "+message);
    }

    }

    I have 1 more class, ECardSeller which i have not start due to stuck bcos of these errors. And another class CarPrinter which is from another different package.

  11. #10
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Calling constructor error

    Spelling and capitalization matters, check to see if your code correctly overrides the abstract method public int compareTo(GreetingCard other); .

  12. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    ok so i know i have to override the abstract method compareTo, but how should i code it in my other extends class?I am a beginner for object oriented programming so pls guide me huhu

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling constructor error

    Can you post your current code in code tags?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    How do i post in code tags?

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling constructor error

    Wrap your code with
    [code=java]

    <YOUR CODE HERE>

    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    [/code]
    import java.util.Date;
    public abstract class GreetingCard implements Comparable {
    String message;
    Date arrivalDate;

    GreetingCard(String message, Date arrivalDate){
    this.message = message;
    this.arrivalDate = arrivalDate;
    }

    public int comPareTo(GreetingCard other){
    if(arrivalDate.equals(other.arrivalDate)){
    return 0;
    }
    else{
    if(arrivalDate.equals(other.arrivalDate)){
    return 1;
    }
    else{
    return -1;
    }
    }
    }

    abstract String formatMessage();
    }

    import java.util.Date;
    public class BirthdayCard extends GreetingCard{

    BirthdayCard(String bm, Date dd){
    super(bm,dd);
    this.message = bm;
    this.arrivalDate = dd;
    }
    public Date arrivalDate(){
    return arrivalDate;
    }
    String formatMessage(){
    return ("Happy Birthday" + this.message);
    }
    }

    import java.util.Date;
    public class Anniversary extends GreetingCard {
    String occassion;

    Anniversary(String am,Date ad){
    super(am,ad);
    this.message = am;
    this.arrivalDate = ad;
    }

    String formatMessage(){
    return("Happy "+occassion+"Anniversary "+message);
    }

    }

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling constructor error

    Please edit your post and wrap the code in code tags. See post#14

    Also correct the spelling in the code as has been mentioned many times already.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    import java.util.Date;
    public abstract class GreetingCard implements Comparable {
        String message;
        Date arrivalDate;
     
        GreetingCard(String message, Date arrivalDate){
            this.message = message;
            this.arrivalDate = arrivalDate;
        }
     
        public int comPareTo(GreetingCard other){
            if(arrivalDate.equals(other.arrivalDate)){
                return 0;
            }
            else{
                if(arrivalDate.equals(other.arrivalDate)){
                    return 1;
                }
                else{
                    return -1;
                }
            }
        }
     
        abstract String formatMessage();
    }
    import java.util.Date;
    public class BirthdayCard extends GreetingCard{
     
        BirthdayCard(String bm, Date dd){
            super(bm,dd);
            this.message = bm;
            this.arrivalDate = dd;
        }
        public Date arrivalDate(){
            return arrivalDate;
        }
        String formatMessage(){
            return ("Happy Birthday" + this.message);
        }
    }
    import java.util.Date;
    public class Anniversary extends GreetingCard {
        String occassion;
     
        Anniversary(String am,Date ad){
            super(am,ad);
            this.message = am;
            this.arrivalDate = ad;
        }
     
        String formatMessage(){
            return("Happy "+occassion+"Anniversary "+message);
        }
     
    }

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling constructor error

    Now you need to correct the spelling in the code as has been mentioned many times already.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    haliza hadi (February 16th, 2013)

  21. #19
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    which spelling do i need to correct and from which class??

  22. #20
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    Quote Originally Posted by haliza hadi View Post
    which spelling do i need to correct and from which class??

    The compiler is telling you exactly what needs to be fixed.

    Again, the compiler is extremely picky, and so you must be just as picky when you create your programs. Your errors are simply from being careless, and we can't teach you to be careful, but instead you must learn this yourself by being constantly vigilant.

    I suggest that if you can't use an IDE such as Eclipse or NetBeans, that you compile frequently when creating programs, that you add code to your programs one to lines at a time, and that each time you follow this with a compilation. If the code doesn't compile, then don't add *any* new lines until you've fixed the compilation errors present.

  23. The Following User Says Thank You to curmudgeon For This Useful Post:

    haliza hadi (February 16th, 2013)

  24. #21
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    ok..i've found the spelling for method "compareTo"

  25. #22
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    I try to work out the error in my child class but to no avail...what does this error mean??
    BirthdayCard is not abstract and does not override abstract method compareTo(Object) in Comparable
    ----

  26. #23
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Calling constructor error

    Did you correct the spelling for the compareTo method? Can you please post the current code that you have in the Code tags?

  27. #24
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    My Mood
    Bored
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calling constructor error

    Yes i did change the spelling for compareTo
     
    import java.util.Date;
    public abstract class GreetingCard implements Comparable {
        String message;
        Date arrivalDate;
     
        GreetingCard(String message, Date arrivalDate){
            this.message = message;
            this.arrivalDate = arrivalDate;
        }
     
        public int compareTo(GreetingCard other){
            if(arrivalDate.equals(other.arrivalDate)){
                return 0;
            }
            else{
                if(arrivalDate.equals(other.arrivalDate)){
                    return 1;
                }
                else{
                    return -1;
                }
            }
        }
     
        abstract String formatMessage();
    }
    import java.util.Date;
    public class BirthdayCard extends GreetingCard{
     
        BirthdayCard(String bm, Date dd){
            super(bm,dd);
            this.message = bm;
            this.arrivalDate = dd;
        }
        public Date arrivalDate(){
            return arrivalDate;
        }
        String formatMessage(){
            return ("Happy Birthday" + this.message);
        }
     
    }
     
    import java.util.Date;
    public class Anniversary extends GreetingCard {
        String occassion;
     
        Anniversary(String am,Date ad){
            super(am,ad);
            this.message = am;
            this.arrivalDate = ad;
        }
     
        String formatMessage(){
            return("Happy "+occassion+"Anniversary "+message);
        }
     
    }


    --- Update ---

    Yes i did change the spelling for compareTo
     
    import java.util.Date;
    public abstract class GreetingCard implements Comparable {
        String message;
        Date arrivalDate;
     
        GreetingCard(String message, Date arrivalDate){
            this.message = message;
            this.arrivalDate = arrivalDate;
        }
     
        public int compareTo(GreetingCard other){
            if(arrivalDate.equals(other.arrivalDate)){
                return 0;
            }
            else{
                if(arrivalDate.equals(other.arrivalDate)){
                    return 1;
                }
                else{
                    return -1;
                }
            }
        }
     
        abstract String formatMessage();
    }
    import java.util.Date;
    public class BirthdayCard extends GreetingCard{
     
        BirthdayCard(String bm, Date dd){
            super(bm,dd);
            this.message = bm;
            this.arrivalDate = dd;
        }
        public Date arrivalDate(){
            return arrivalDate;
        }
        String formatMessage(){
            return ("Happy Birthday" + this.message);
        }
     
    }
     
    import java.util.Date;
    public class Anniversary extends GreetingCard {
        String occassion;
     
        Anniversary(String am,Date ad){
            super(am,ad);
            this.message = am;
            this.arrivalDate = ad;
        }
     
        String formatMessage(){
            return("Happy "+occassion+"Anniversary "+message);
        }
     
    }

  28. #25
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calling constructor error

    Again the compiler is telling you exactly what is wrong. It's saying that compareTo must accept an Object parameter, while you've written it to accept a GreetingCard parameter. You can do one of two things:
    1. Either change your compareTo method to one that accepts an Object parameter. If you do this, then inside the method you'll have to assign the parameter into a GreetingCard variable and will need to cast it to GreetingCard to do this.
    2. Or have your class implement the generic version of the interface, Comparable<GreetingCard>.


    Regardless, remember that the compiler error messages are usually very helpful in figuring out a problem and in solving it.

  29. The Following User Says Thank You to curmudgeon For This Useful Post:

    haliza hadi (February 18th, 2013)

Page 1 of 2 12 LastLast

Similar Threads

  1. Beginner assignment - constructor issue / error
    By Jarruda in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2012, 01:29 AM
  2. [SOLVED] Error in calling JFrame's super.
    By CurseControl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 20th, 2012, 09:53 PM
  3. [SOLVED] Error in constructor
    By hello_world in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 14th, 2011, 07:46 PM
  4. Junit3 error: Implicit super constructor TestCase() is not visible
    By albertkao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 12:53 PM
  5. calling a constructor
    By turnwellm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2010, 08:46 PM