When to use Classes vs Objects vs Case Classes vs Traits

This is what I understood so far.

Classes should be used when we need instantialte objects.

We use "Objects" only when we have a singleton requirement, meaning that there is no need for multiple instances of "objects". I think that when we create common library functions or utility functions in a project, we can use "objects" in such a way that we need to create instances every time we use methods / functions in this object.

Classes of classes can be used when we want to save boilerplate code. Say we have a class of "Stock". Each stock as a whole has hundreds of member variables. Instead of writing developer code for setters and getters, if we have a case class, it generates a lot of code by default.

Features: do not know when to use. Both parameters and objects do not accept parameters during initialization.

Any thoughts and ideas are kindly shared.

+4
source share
1 answer

In Scala, classes act like in any other OO language (for example, Java) - you use classes to create objects (not Scala Objects) in your applications that have state (for example, saving attributes, say, the name of the Person).

Scala - . Scala

  • /, / . OO- static.
  • factory ( apply) (. Scala -)

(, datacontainers). ( /), . , , .

Java . /.

Scala, OO.

+8

Source: https://habr.com/ru/post/1673827/


All Articles