2 - OOP in Scala

Today we review OOP principles and apply them in Scala.
We also show why Scala is more OO than Java :)

You can find today's contents here.


You can create a simple project, with a single Scala file. Call it whatever you like.
Along with the main method, you can create a method for every exercise.

  1. A simple class - Define a simple class Car that has two fields speed and weight and a method accelerate
    • The method should just print something.
  2. Car Factory - Assume you have the abstract class Car which is extended by SuperCar and MuscleCar
    • Implement a CarFactory class for it. It should define a getCar method that takes a carType argument
    • Hint: you can use case objects to define the car type (and try some pattern match)
  3. Companions - Instead of defining a separate CarFactory, refactor the previous solution and move the getCar method within the Car's companion object. Companions are usually used to define constants or methods resembling the static ones in Java.
sesiuni/scala/lab2.txt · Last modified: 2016/06/28 15:28 by dciocirlan