Scala Class new

Scala is a language that join both worlds “Functional Programming” and “Object Oriented”.

We talked a little bit about “Functional Programming” in the previous SKBs and today we are going to introduce the basics for “Object Oriented” : the classes and more specifically how to create an instance of a class.

We will be diving a bit more into the theory after the exercise.

Aclassis a collection of functions that can be reused. This collection is created with parameters and that will make theinstanceof the class behave differently.

In a lot of programming books, you will see aclassbeing described as a “blueprint”. And using this “blueprint”, you are able to “instantiate”, or build, anobject.

The way to build a class in Scala is using the keywordnew.

To summarize all the terminology:

  • Class: A collection of methods, values. Built with parameter(s). Instantiated to an object via the keywordnew.
  • Object: An instance of a class built with specific parameters.
  • Instantiate: The action of creating an object from a class.
  • new: The keyword used in Scala to instantiate a class and make an object.
  • Field: Thevalcontained in theclass.
  • Method: Thedef(functions) contained in theclass.
  • Member: A term to describe bothfieldandmethod.

Reveal more information and clues
Load Exercise