I am new to Java and I am learning the basics. I was studying the toString
method and how to override it in my own classes. I am just wondering why has toString
to be public
? is it because it is defined so in the Object
class?
-
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3 – Matt Ball Mar 17 '13 at 23:24
-
You can always call a private method from within `toString()` – Moak Mar 22 '13 at 03:17
7 Answers
From official Oracle documentation:
Modifiers
The access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass.
This because inheritance creates an IS-A relation between two classes, for which the Liskov substitution principle must be valid. Without having the previous constraint that would be impossible to enforce.
-
The LSP requires that casting an object to a base type makes it behave as a base type, so any object which overrides a method must do so in such a fashion as to comply with the base-class contract. *That does not imply that the method must be useful, however*. If the `Woozler` contract specifies that `Fnorble()` is not required to do anything useful if `CanFnorble()` returns false, and if sealed class `Moozler` (derived from `Woozler`) cannot `Fnorble()`, then it would be just as well to forbid attempts to call `Fnorble()` on an object which is known to be a `Moozler`. – supercat Dec 19 '13 at 07:55
Think about it: You subclass Gizmo with MyGizmo. This means that any place that a Gizmo can be used, you can use a MyGizmo in it's place. If some program does gizmoObject.toString()
then that should work even if gizmoObject is not a Gizmo but a MyGizmo.
In particular, toString
is used for printing & dumping objects and needs to be accessible on all objects.

- 47,103
- 17
- 93
- 151
-
+1, for the same reason as @OliCharlesworth. It is not just an arbitrary rule. – Patricia Shanahan Mar 17 '13 at 23:31
When you override any method, the visibility of the override must be at least as visible as the base method.

- 868,454
- 176
- 1,908
- 1,964
When you override any method, the visibility of the override must be equal or more than the visibility of the base method

- 4,511
- 8
- 47
- 81
When overriding a method, you should declare the visibility of overridden method as the the one in the base class or wider.
Here is a note about the overriding rules I wrote it myself as a reference to me:
/* Access Modifiers */
1- Access modifier in child should be same or greater (the sequence: public - protected - default (package-private) - private). Access modifier in parent should not be private. If a child don't have access to the parent's method, then overriding the method without the @Override annotation will consider it as a new method.
/* Return Types */
2- Return type in child should be the same class or sub-class.
/* Exceptions */
3- If the parent's method throws checked exception, then the child's method can: a- throw the same exception or sub-class. b- not to throw any exceptions. c- throw runtime exceptions.
4- If the parent's method throws unchecked exception, then the child's method can: a- not to throw any exceptions. b- throw runtime exceptions.
5- If the parnet's method does not throw exception, then the child's method can: a- not to throw any exceptions. b- throw runtime exceptions.

- 115,165
- 71
- 313
- 417
Because you are overriding a method, you must define it as the prototype method or more than that.

- 9
- 1
-
-1: Beside the point (`toString` must be `public`, `protected` won't do), and incorrect (you can override a method declared with access modifier with another method without access modifier if it is in the same package) – meriton Mar 22 '13 at 19:24
when you override an sub class method visibility must be wider than parent class.
Wider to strict order:
public default protected private

- 400
- 1
- 2
- 12