# toString - enforcing overriding

**URL:** <https://users.scala-lang.org/t/tostring-enforcing-overriding/5166>\
**Category:** Question\
**Created:** [October 9, 2019, 12:53pm UTC](https://users.scala-lang.org/t/tostring-enforcing-overriding/5166 "2019-10-09T12:53:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stairs](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@stairs](https://users.scala-lang.org/u/stairs)\
**Post date:** [October 9, 2019, 12:53pm UTC](https://users.scala-lang.org/t/tostring-enforcing-overriding/5166/1 "2019-10-09T12:53:24Z")

</div>

I have a style question:

Every class gets a `toString` from `Any`. However, when declaring an abstract class from which you might generate a number of subclasses, do people tend to add in a `toString` method at that `abstract class` level to enforce the requirement that users override the `toString` method, rather than relying on remembering to do so? Or does this add extra clutter to the abstract class?

---

<div class="post-metadata">

**Author:** ![crater2150](https://users.scala-lang.org/user_avatar/users.scala-lang.org/crater2150/32/1011_2.png) [@crater2150](https://users.scala-lang.org/u/crater2150)\
**Post date:** [October 9, 2019, 1:36pm UTC](https://users.scala-lang.org/t/tostring-enforcing-overriding/5166/2 "2019-10-09T13:36:54Z")

</div>

Personally, in such cases I used a differently named method. A subclass may still decide to just call toString in it, but in cases where all subclasses of a class need a `toString`, there usually is a better name.

If you require the `toString` method to be overloaded, there’s a good chance anyway, that you also need a specific format, and in that case I wouldn’t want the method to be available on all types, even the ones that are not subtypes of my abstract class. That’s also one of the main reasons why I switched to using the `Show` typeclass from cats, because it’s only available for types that actually do implement it and does not have a default for all others.

Edit: oh, another point: case classes will generate a `toString`, which will count as overriding, so you can still forget to override it, although the default will be more useful.
