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

wrong types in java

Options
  • 09-12-2003 12:40pm
    #1
    Closed Accounts Posts: 1,152 ✭✭✭


    i have overridden a method but i cnat get my code to compile

    the method in the super class returns an integer while the method in the subclass has to return a string which concatenates information.

    i have tried manythings like integer.tostring() but to no avail.

    the code for the two methods are below . any help would be much appreciated as my proj is due in tomorrow

    ==========================================================
    method in superclass :

    String getDetails() //method to return details
    {
    return age;

    }//end getDeatils

    method in subclass:

    String getDetails()
    {
    String concat = Integer.toString(age) + changeSubject();
    return concat; //returns concat

    }
    =======================================================


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    would this work?

    return "" + age;

    ?


  • Registered Users Posts: 1,865 ✭✭✭Syth


    If age is declared as private in the superclass then you wouldn't be able to access it directly. It'd only be accessable if it was declared public or protected.

    Mabye try:
    String concat = Integer.toString(super()) + changeSubject();
    or
    String concat = (String)super() + changeSubject();

    What exaclty happens when you try to run or compile this code?

    <edit>Thank's bonkey.</edit>


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by sound_wave
    the method in the super class returns an integer while the method in the subclass has to return a string which concatenates information.

    But in the code you present, the method in the superclass does not return an integer. It returns a string....at least going by how you present the code below.

    age may be an Integer / int, but you have the method defined with a return-type of String.

    If thats a typo here, and in actual fact the method in your code has a return type of int/Integer, then I think thats the source of your problems. I don't think java lets you override without having an identical calling interface.

    I guess it would also be of help if you mentioned what is being complained about - the method declaration, or the return statement - and ideally include a sample of the compiler error output


    ==========================================================
    method in superclass :

    String getDetails() //method to return details
    {
    return age;

    }//end getDeatils

    method in subclass:

    String getDetails()
    {
    String concat = Integer.toString(age) + changeSubject();
    return concat; //returns concat

    }
    ======================================================= [/B][/QUOTE]


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by Syth
    If age is declared as private in the superclass then you wouldn't be able to access it directly. It'd only be accessable if it was declared public or private.

    Methinks that second private should read "protected". No?

    jc


  • Registered Users Posts: 4,666 ✭✭✭Imposter


    Return types must be the same for overridden methods in Java.

    You could change the interface to work with an Object which would return whichever type you want each time.

    Doing a check on the returned object using instanceof can be used to cast back if you need the specific type.


  • Advertisement
  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    thanks for all the help lads. got it compiled and running without any errors.

    thanks once again


Advertisement