14

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?

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
mikey
  • 1,339
  • 5
  • 22
  • 43

7 Answers7

28

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.

Community
  • 1
  • 1
Jack
  • 131,802
  • 30
  • 241
  • 343
  • 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
22

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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
14

When you override any method, the visibility of the override must be at least as visible as the base method.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

When you override any method, the visibility of the override must be equal or more than the visibility of the base method

Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
1

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.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

Because you are overriding a method, you must define it as the prototype method or more than that.

Yuqian
  • 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
0

when you override an sub class method visibility must be wider than parent class.

Wider to strict order:

public default protected private

Tushar Trivedi
  • 400
  • 1
  • 2
  • 12