Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (myname):

Question in Java --> I have this method in on of my classes ---------->public static String toString(String s) While i try to instantiate this class and run a program it says cannot override toString() ?

OpenStudy (myname):

I am confused as i haven't done any abstraction?

OpenStudy (anonymous):

There already exists a method with the name toString. Try changing the name of the method.

OpenStudy (anonymous):

java.lang package has a method named toString ,so you cannot override the method to perform your own operations because you have marked it static , remove static declaration and then try to compile

OpenStudy (goformit100):

correct answer yaar

OpenStudy (anonymous):

None of the above answers truly address your question. First note that every class in Java inherits from java.lang.Object, which declares a method with the following descriptor: public String toString() The specific problem at hand is that, by declaring your own method with the same name: public static String toString(), you've created an naming collision that javac reports to you. In Java source code, two methods may only share the same name if they differ in their parameters, to prevent ambiguity like in the following case: class A { Integer b(); String b(); } ... A inst = new A(); Object value = inst.b(); Note that it cannot be determined which method was meant to be called, neither by a compiler nor a human. Your solution is to rename your method to something like: public static String toText(); Good luck.

OpenStudy (myname):

Thank you oldrin.bataku.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!