+ 1

Type of toString() method?

Hi all I use toString() method to print my property on objects. If default type of methods is "public" ,when i not type it on first line define method why get error? Code: String toStding() { return x; }

15th Jun 2019, 1:36 PM
Hadi
Hadi - avatar
6 ответов
+ 5
Hadi I don't know where you write your codes. If you are not using sololearn you can create packages. If you do it or not does not matter how the access modifier works. My last post was just to explain you that there is a difference between default and public, which you can't see on sololearn. It is the same with using one class. You can use the keyword private for everything. You will not see a difference between private and public. But that does not mean that private = public. Why can't you change public to default: " Overriding and Access-Modifiers : The access modifier for an overriding method can allow more, but not less, access than the overridden method." Overriden method is public -> use also public default -> default or public protetected -> protected/default/public private -> all access modifier allowed Here you find all overriding rules: https://www.google.de/amp/s/www.geeksforgeeks.org/overriding-in-java/amp/
15th Jun 2019, 3:29 PM
Denise Roßberg
Denise Roßberg - avatar
15th Jun 2019, 3:32 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Because default is not public. public means the method/class/variable is visible from everywhere (inside the class, outside the class and outside the package). private should be clear -> only inside the class default: inside and outside the class but not outside the package. default String toString() has weaker access than public String toString(). Here is a graphic: https://i.stack.imgur.com/E5c6l.png
15th Jun 2019, 2:04 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Btw: Sololearn does not support packages. So all code uses one package -> default looks like public But if you use an IDE on your PC you can write code (package a) and write another code (package b) which imports package a. If in a is a default method -> b has no access. b has only access if the method is public.
15th Jun 2019, 2:15 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Denise Roßberg But i use no IDE and all classes is on subfolder
15th Jun 2019, 3:04 PM
Hadi
Hadi - avatar