The first example prints a String to the console but that is not the same as returning a String.
Remember, printing out a value (which is what System.out.println does) is a completely different concept to returning a value.
Your method signature says it must return String. You must therefore always return a String.
Anyway, your new question:
The above code gives error:"toString() in InterfaceTest cannot override toString() in Object void toString() attempting to assign weaker access privileges; was public"
public String toString() is a method declared in the Object class. There are two problems with your first example. You override it with weaker access privileges (if you override a public method it needs to be public) - that's the error message you're getting. And you override it with a different return type (void). Neither of these are allowed.
In your second example you don't do either of these, so no problem.
Robby Ames wrote:Here is the exact question: http://www.coderanch.com/t/600411/java/java/needed-Interfaces-Java#2739533
You purposely duplicated this exact question? May I ask why?
I didn't know that 'toString()' is an internal function so now I changed it with function 'Dsply()' (please see the first part of both questions).
Therefore, the question is totally different now. The latest post has been blocked(thinking it's the same post).
Please help me to find the answer.
Also, Disply is a really poor name for a method. Firstly, methods should begin with a lowercase letter. Secondly, vowels are not expensive. display is a much better name.
Robby Ames wrote:I purposely didn't give the error message in the new post(because it will lead to many different opinion).
So the question is same. The two similar programs are there but one is giving error and other is not.
Are you purposely making it hard to help you, or making people not want to help?
1) Keep the problem to one thread
2) Don't change your post after people have responded
3) State your problem clearly
In the other post you do not post the error you get. So there is still no indication of what the problem is. I know from reading the code, but I refuse to answer until you tell us what the error is, as has been requested already.
The above code gives error: "cannot find symbol obj.display();"
The code below runs smoothly!!
Why isn't the 2nd code giving error?
(I hope, now everything is right. Thank you guys for your concern.)