Let us revise few of the important basics of Object Oriented Programming. In case, you are newbie for Scala and want to learn it in detail, please go through this course. For CCA 175, object oriented programming is not important.
Let us see how we can define class in Scala.
- By default all the arguments passed to class are constructor arguments
- All the free flowing code with the class body is constructor behavior.
- In case if you want to create class level variables, you need to use val or var
- Class methods can be defined with in the class body
- apply is special method where we cannot use method name while invoking
- There are few other special methods in Scala classes
Objects are special types of classes.
- It is singleton class
- main function is defined in object
- If the class name and object name are same then they are called as companions
- We cannot instantiate object
Case class is special type of class, where we get boiler plate code
- It contains companion object with apply method
- Default constructor will be provided for case class
- It implements Product and Serializable, which gives us functionality for methods such as productArity, productElement etc
- By default, arguments passed to case class will be immutable variables (val). We will get getter methods for all the variables
- If we define arguments as mutable variables (var), we will get functionality for both getter and setter methods.
- There are few other methods for which boiler plate code is generated
- and more