DevOps Articles

Curated articles, resources, tips and trends from the DevOps World.

The Practical Difference Between Abstract Classes and Traits in Scala

4 years ago dzone.com
The Practical Difference Between Abstract Classes and Traits in Scala

Summary: This is a summary of an article originally published by the source. Read the full original article here →

This short article will compare abstract classes and traits as means of inheritance.

If you're trying to figure out which is the best way to create OO-style type hierarchies and abstract classes seem too similar to traits, this article is for you.

So from this point of view, a trait and an abstract class are identical.

One of the fundamental differences between a trait and an abstract class is that multiple traits may be used in the inheritance definition of a class, whereas a single abstract class can be extended.

A second, more practical difference between a trait and an abstract class is that an abstract class can take constructor arguments: whereas traits cannot: That is, at least until Scala 3 comes out - Scala 3 will also allow constructor arguments for traits.

Made with pure grit © 2024 Jetpack Labs Inc. All rights reserved. www.jetpacklabs.com