Why my overriding method of toString() has to be public?
Asked Answered
D

7

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?

Dialectologist answered 17/3, 2013 at 23:19 Comment(2)
docs.oracle.com/javase/specs/jls/se7/html/…Seclusive
You can always call a private method from within toString()Sikh
C
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.

Cycloparaffin answered 17/3, 2013 at 23:23 Comment(1)
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.Tradition
B
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.

Borchers answered 17/3, 2013 at 23:27 Comment(1)
+1, for the same reason as @OliCharlesworth. It is not just an arbitrary rule.Muddy
A
14

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

Acquaint answered 17/3, 2013 at 23:21 Comment(0)
M
2

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

Margarine answered 17/3, 2013 at 23:23 Comment(0)
M
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.

Macrophage answered 17/3, 2013 at 23:21 Comment(0)
Q
0

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

Quadruplicate answered 17/3, 2013 at 23:23 Comment(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)Mechanism
S
0

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

Wider to strict order:

public default protected private

Sonora answered 18/3, 2013 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.