# Make concrete classes final by default

**URL:** <https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058>\
**Category:** Language Design\
**Created:** [July 16, 2018, 12:54am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058 "2018-07-16T00:54:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshlemer](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/joshlemer/32/39_2.png) [@joshlemer](https://contributors.scala-lang.org/u/joshlemer)\
**Post date:** [July 16, 2018, 12:54am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/1 "2018-07-16T00:54:54Z")

</div>

Extending concrete classes is permitted in Scala, unless the class is marked by a `final` modifier, however actually extending concrete classes is pretty rare in Scala, and is widely regarded as a code smell in most styles of programming in Scala. I think that in keeping with Scala’s philosophy of nudging programmers towards better practice by default, it’s a good idea to make concrete classes final by default unless opted out of. I think that this will be a very non-controversial issue as well, since both OOP and FP camps both consider concrete inheritance a smell (at the least). I’ve seen talks about FP in scala for instance where every case class is labelled `final`.

For inspiration you can look at what Kotlin has done. In that language, all concrete classes are final unless marked `open`. It would look like this:

```scala
open class A
class B extends A
class C extends B // compile error

```

Likewise, one could go even further, as Kotlin has, and make concrete methods final unless also declared `open`.

```scala
trait A {
  def x: Int
  def y: Int = 1
  open def z: Int = 2
}

class B extends A {
  def x = 0 // ok
  override def y = 2 // NOT OK 
  override def z = 5 // OK 
}

```

---

<div class="post-metadata">

**Author:** ![NthPortal](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nthportal/32/1845_2.png) [@NthPortal](https://contributors.scala-lang.org/u/NthPortal)\
**Post date:** [July 16, 2018, 3:00am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/2 "2018-07-16T03:00:08Z")

</div>

I’m am quite in favour of making things final by default (also private, for that matter). However I have a feeling this would break a LOT of code (though it seems like a trivial rewrite to retain current semantics). I’m not sure about methods being final by default, but certainly classes. There are a LOT of bugs that would be fixed by that (e.g. `::` being extendable in 2.12)

For reference, Rust also follows this philosophy.

---

<div class="post-metadata">

**Author:** ![joshlemer](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/joshlemer/32/39_2.png) [@joshlemer](https://contributors.scala-lang.org/u/joshlemer)\
**Post date:** [July 16, 2018, 3:04am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/3 "2018-07-16T03:04:41Z")

</div>

> [@NthPortal](#):
>
> For reference, Rust also follows this philosophy.

@NthPortal just to clarify, here you’re talking about private-by-default right? There’s no such thing as overrides in Rust is there?

---

<div class="post-metadata">

**Author:** ![NthPortal](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nthportal/32/1845_2.png) [@NthPortal](https://contributors.scala-lang.org/u/NthPortal)\
**Post date:** [July 16, 2018, 3:07am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/4 "2018-07-16T03:07:59Z")

</div>

I just mean the maximum-safety-by-default philosophy. This includes private by default, immutable by default, and probably other things I haven’t thought of. You are correct that Rust (at least AFAIK) does not have inheritance or overriding

---

<div class="post-metadata">

**Author:** ![dwijnand](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/dwijnand/32/796_2.png) [@dwijnand](https://contributors.scala-lang.org/u/dwijnand)\
**Post date:** [July 16, 2018, 9:27am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/5 "2018-07-16T09:27:59Z")

</div>

In addition to my 👍 (I mean ❤), to continue to brainstorming here, I assume adding `abstract` or `sealed` would implicitly make a class `open`.

---

<div class="post-metadata">

**Author:** ![joshlemer](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/joshlemer/32/39_2.png) [@joshlemer](https://contributors.scala-lang.org/u/joshlemer)\
**Post date:** [July 16, 2018, 11:07am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/6 "2018-07-16T11:07:59Z")

</div>

> [@dwijnand](#):
>
> I assume adding `abstract` or `sealed` would implicitly make a class `open` .

You are correct in the case of `abstract`, this proposal is only about finalizing concrete classes, and abstract classes are not concrete.

However in the case of `sealed`, I don’t think this necessarily goes without saying. Almost all uses of `sealed` today are on `sealed trait` and `sealed abstract class` which would remain open to extension, but you can also have a `sealed class` which is concrete. I guess it’s up for debate whether to make sealed concrete classes open by default, but it could easily go the other way too.

---

<div class="post-metadata">

**Author:** ![japgolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/japgolly/32/140_2.png) [@japgolly](https://contributors.scala-lang.org/u/japgolly)\
**Post date:** [July 16, 2018, 11:38am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/7 "2018-07-16T11:38:39Z")

</div>

In my opinion, everything should be final by default: classes, traits, def members, val members, type members.

---

<div class="post-metadata">

**Author:** ![joshlemer](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/joshlemer/32/39_2.png) [@joshlemer](https://contributors.scala-lang.org/u/joshlemer)\
**Post date:** [July 16, 2018, 11:39am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/8 "2018-07-16T11:39:41Z")

</div>

I don’t think it makes much sense to make traits final by default – their whole point is to be extended/implemented.

---

<div class="post-metadata">

**Author:** ![Krever](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/krever/32/1696_2.png) [@Krever](https://contributors.scala-lang.org/u/Krever)\
**Post date:** [July 16, 2018, 11:57am UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/9 "2018-07-16T11:57:53Z")

</div>

A question to more experienced fellows here: isn’t it restricting extensibility? Right now if there is a bug or other unwanted behaviour in a class exposed from library anyone can override the behaviour and continue using it immediately even if author has not anticipated such use case.

---

<div class="post-metadata">

**Author:** ![eyalroth](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/eyalroth/32/851_2.png) [@eyalroth](https://contributors.scala-lang.org/u/eyalroth)\
**Post date:** [July 16, 2018, 12:37pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/10 "2018-07-16T12:37:16Z")

</div>

I mostly agree with the argument that extending a concrete class is a bad-practice in most cases; however, I still oppose to making this the default behavior for the following reasons:

1. This would probably break **A LOT** of code (mentioned by @NthPortal as well).
2. Would harshly restrict extensibility of external libraries (mentioned by @Krever as well). In my experience I’ve come to override plenty of external concrete classes for the sake of better usability / bug fixes.
3. This is the opposite of how Java behaves, which I fear would lead to quite a lot confusion for newcomers, especially in mixed Java/Scala projects.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [July 16, 2018, 1:05pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/11 "2018-07-16T13:05:51Z")

</div>

Well, if we view the ability to patch external code by extending a class as a feature, perhaps we should remove `final` from the language. Cause right now the only reason you can do that kind of patching is because the author forgot to make his class `final`. Otherwise if it’s his intention that clients can do this he would make his class `open` in this new scheme.

That said, completely inverting open-final and/or private-public semantics is a huge change I’m not sure you can pull off after a language has existed for 15 years

---

<div class="post-metadata">

**Author:** ![btmcnellis](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/btmcnellis/32/885_2.png) [@btmcnellis](https://contributors.scala-lang.org/u/btmcnellis)\
**Post date:** [July 16, 2018, 2:57pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/12 "2018-07-16T14:57:44Z")

</div>

If we were starting from scratch, I, and it seems like a lot of others here, would definitely agree with this. However, I’d like to echo the sentiment that this is too significant a breaking change to introduce at this point.

To provide a concrete example of why this is a problem, Mockito cannot mock final classes by default. It can be configured in recent versions by adding a configuration file to the classpath, but even then there are limitations when it comes to certain settings and alternative JVM implementations. See [http://static.javadoc.io/org.mockito/mockito-core/2.19.1/org/mockito/Mockito.html#39](http://static.javadoc.io/org.mockito/mockito-core/2.19.1/org/mockito/Mockito.html#39)

(Yes, I realize that there are problems with using Mockito, especially if you’re trying to use it in a functional context, but the fact is a lot of real-world code does use it. I know that my company would definitely be impacted heavily by this change.)

---

<div class="post-metadata">

**Author:** ![eyalroth](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/eyalroth/32/851_2.png) [@eyalroth](https://contributors.scala-lang.org/u/eyalroth)\
**Post date:** [July 16, 2018, 3:12pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/13 "2018-07-16T15:12:42Z")

</div>

> [@btmcnellis](#):
>
> (Yes, I realize that there are problems with using Mockito, especially if you’re trying to use it in a functional context, but the fact is a lot of real-world code does use it. I know that my company would definitely be impacted heavily by this change.)

I would actually argue that Mockito is the way to go about mocking in Scala as well 😉 I had too many problems with ScalaMock.

---

<div class="post-metadata">

**Author:** ![ghik](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ghik/32/1954_2.png) [@ghik](https://contributors.scala-lang.org/u/ghik)\
**Post date:** [July 16, 2018, 3:16pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/14 "2018-07-16T15:16:54Z")

</div>

I absolutely support the idea that everything non-abstract should be final by default, because 90% of the time this is programmer’s intention. However, something as fundamental as this is probably impossible to change for Scala now. Kotlin did it right by introducing `open` at the very inception of the language.

---

<div class="post-metadata">

**Author:** ![kevinwright](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/kevinwright/32/588_2.png) [@kevinwright](https://contributors.scala-lang.org/u/kevinwright)\
**Post date:** [July 16, 2018, 8:58pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/15 "2018-07-16T20:58:29Z")

</div>

I can see some argument for doing this at the birth of the language, but now… not a chance!

The cost/benefit simply doesn’t justify it.

It’s impossible to know when/how another developer might need to use/extend your class. Yes… there are certain places where it’s very high-risk, and we have the `final` keyword for that. There’s also an argument for encouraging composition over inheritance, but more than once I’ve been in a place where it turned out that I could have easily solved a problem with access to some protected member and cursed whoever made the class final.

Compared to such weak benefits, the cost of potentially breaking a **lot** of existing code is very hard to justify.

There’s also no performance argument to be had here, as the JVM is already **very** good at identifying “effectively final” classes for optimisation reasons.

For case classes though? There’s much stronger justification for making them final by default, but that’s the sort of change that can’t be made until Scala 3, and even then the potential cost of introducing a new keyword to override this behaviour must be weighed up very carefully indeed. After all… `open` is used in a staggering amount of IO and network libraries!

---

<div class="post-metadata">

**Author:** ![Ichoran](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ichoran/32/40_2.png) [@Ichoran](https://contributors.scala-lang.org/u/Ichoran)\
**Post date:** [July 16, 2018, 10:11pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/16 "2018-07-16T22:11:11Z")

</div>

I think this is the wrong choice because it is hard to avoid letting implementation details leak into your abstraction. Leaking details without actually providing the ability to manipulate or alter the details is incredibly frustrating. If you’re going to go to the effort to think carefully enough to hide the details appropriately, you can throw a `final` here and there on your internal methods to keep frozen what needs to be kept frozen.

If everything is immutable anyway, it’s less of an issue, but the cost of having something overridable is pretty low. Occasionally someone without much experience gets confused about how objects are remade, overrides something, and wonders where it went. Lesson learned, no more problem.

(Rust is different because there’s no inheritance, so `final` doesn’t apply!)

It seems like a combination of bad practice and solving a problem that doesn’t exist to make this change.

Not to mention that it’s a massive breaking change that is completely infeasible, but even if it weren’t, I think it would be the wrong thing to do.

On case classes, since overriding is basically deprecated anyway, I agree that requiring the `final` is mostly irritating with no benefit. That could perhaps be changed someday.

---

<div class="post-metadata">

**Author:** ![smarter](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/smarter/32/92_2.png) [@smarter](https://contributors.scala-lang.org/u/smarter)\
**Post date:** [July 16, 2018, 10:16pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/17 "2018-07-16T22:16:26Z")

</div>

I’m not sure this change is worth the pain, but we could avoid breaking code if it was done through a long enough cycle, e.g:

- In Scala N introduce `open class` to mean the same thing as `class`, also introduce a warning if `class` is used by itself asking the user to rewrite it as `open class` or `final class`
- In Scala N+X, make it an error to write `class` without prefixing it by `open` or `final`, unless the flag `-Xsource:N+X+1` is used, in which case `class` now means `final class`.
- In Scala N+X+1, reintroduce `class` to mean the same thing as `final class` by default.

---

<div class="post-metadata">

**Author:** ![japgolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/japgolly/32/140_2.png) [@japgolly](https://contributors.scala-lang.org/u/japgolly)\
**Post date:** [July 16, 2018, 11:34pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/18 "2018-07-16T23:34:07Z")

</div>

Haha yes, you’re right. A little bit over zealous without properly thinking there. But you know what I mean I hope 🙂

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [July 16, 2018, 11:46pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/19 "2018-07-16T23:46:19Z")

</div>

> [@Ichoran](#):
>
> If everything is immutable anyway, it’s less of an issue, but the cost of having something overridable is pretty low.

The cost is actually pretty big, when you consider the _evolution_ of a library over time while preserving backward binary compatibility (as we always should do). As a library maintainer, I have more freedom in the changes I apply to final classes than to non-final ones. For that reason, we have a design guideline in Scala.js that everything must be final or sealed, unless explicitly intended to be extended.

Oh and because someone is bound to answer my comment with something like “one more reason to do source dependencies instead”, please don’t: maintaining backward _source_ compatibility in a non-final class is much much much harder than maintaining binary compat.

---

<div class="post-metadata">

**Author:** ![kevinwright](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/kevinwright/32/588_2.png) [@kevinwright](https://contributors.scala-lang.org/u/kevinwright)\
**Post date:** [July 16, 2018, 11:48pm UTC](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058/20 "2018-07-16T23:48:27Z")

</div>

We **could** do that, but only if there’s a sufficiently good enough reason for choosing to make it the default behaviour in the first place.

I don’t think that’s yet been demonstrated, and “being more like Kotlin” is not a sound design strategy… If anything, the language contains a number of questionable anti-patterns that we should seek to actively avoid.

[Next page](https://contributors.scala-lang.org/t/make-concrete-classes-final-by-default/2058.md?page=2)
