Scala — OOP Basic

Object-Oriented Basics

Iris S
2 min readJan 9, 2023

Constructors

class Person(name: String, age: Int) // This is the constructor

val person = new Person("iris", 99)// a new person is constructed

Difference between class parameter and class member

  • class parameters are not fields
  • class members can be accessed by a “.”
  • to convert parameter > field: var & val definition inside of class are fields

“This” keyword

  • “this” access the method field incase there are same class property name and method property name

Overloading

  • overloading — defining method with the same name but different signature.
  • compiler will get confused when fields are same, but return type is different.
  • used def this() to overload constructor

Immutability

  • extremely important in functional programming
  • says that an instance is fixed, and cannot be modified.
  • whenever we need to modify the content of an instance, we need to return a new instance with the modified content

Syntactic Sugar: Method Notations

when an apply() method is defined, when a class is called as a method, the compiler looks for the definition of apply() inside of the class, and execute.

Scala Objects

  • class level functionality (static)
  • means an instance of the object can access the class level definition
  • scala does not have class level functionality
  • in scala, to use class level definition, we use object as a singleton instance. for example, we define the type of the person object, and that is the only instance.
  • the advantage: scala objects are singleton instance by definition
  • a common pattern — companions:

In order to separate class and instance level functionality

  • companion design pattern — write object and class in the same file or same scope to split static(class) and instance level functionality

Factory Method

  • inside an object. to build an object like a constructor

Inheritance

  • Scala offers single class inheritance
  • private will mark a method within its own class
  • protected will mark a method as accessible within its own and subclasses
  • seal will mark a method as accessible within the same file ONLY

Overriding

  • directly specify in the constructor
  • or overriding inside the class definition
  • all instances of derived classes will use the most recent definition

Polymorphism

  • a method call will go to the most overridden definition

Prevent override

  • use final keyword on member to prevent override
  • use final on entire class
  • seal class

Abstract data types

Meaning of abstract

  • for the situation we leave the definition of method and field blank or unimplemented
  • abstract class cannot be inistantiate
  • to extend an abstract class, provide a definition
  • both abstract classes and traits can have either abstract or non-abstract members

Trait

  • the ultimate abstract data type
  • describe behavior of a class
  • can be inherited along with classes
  • do not have constructor parameters

--

--