• Post Reply Bookmark Topic Watch Topic
  • New Topic

How is this possible? (Java, Interface)  RSS feed

 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above code gives error:"toString() in InterfaceTest cannot override toString() in Object void toString() attempting to assign weaker access privileges; was public"
BUT
The code below run smoothly!!

WHY?? (I put the wrong question earlier but now I edited)
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the toString() method is declared as having a return type of String but your first example doesn't return a String object, your second example does.

The first example prints a String to the console but that is not the same as returning a String.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's nothing complicated. In your first example you have a toString() method that is declared to return a String. But you don't return anything. That's the cause of the error. In the second example your toString() method returns a value, so no problem.

Remember, printing out a value (which is what System.out.println does) is a completely different concept to returning a value.
 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you assuming that println returns a String to the method caller? Read java api for that println command.

Your method signature says it must return String. You must therefore always return a String.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't edit your original post to ask a different question. It means the replies you've already got no longer make sense.

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
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Matthew..
Please don't edit your original post to ask a different question. It means the replies you've already got no longer make sense.

Ok I'll remember! Now I'm posting the exact question as a new post please reply there..
 
Robby Ames
Ranch Hand
Posts: 45
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew has already answered this question, why are you starting a new thread?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You purposely duplicated this exact question? May I ask why?

Henry
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are different. Please see closely
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).
Old:

New:

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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't really outlined what the problem is. If you are getting an error, post the exact text of the error message.

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
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

WHY?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

WHY?


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.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't want that but it happened. But any way I'm putting the question again(with more clarity):

The above code gives error: "cannot find symbol obj.display();"
BUT
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.)
 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The display() method isn't declared in the Inter interface and so it's not visible to a variable of type Inter.

If you cast it to InterfaceTest or store it in a variable of type InterfaceTest it will work. The second one works because all objects have a toString() method.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!! Tony..
"The second one works because all
objects have a toString() method."

I get it. Thanks! Again!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!