OOP

  • Scala extends the idea of interfaces introducing traits. A trait is something in between an interface, because it states the nature of methods and even fields, and a full superclass, since a trait can also implement methods and fields.

    Traits are Scala answer to the multiple inheritance problem. Other languages like C++ allow a class to inherit from multiple classes. When two or more superclasses declare the same method, the subclass is forced to reimplement that method to clarify what it does. Java on the other hand excludes multiple inheritance to avoid this kind of complexities. A Java class can only extend one single superclass. Scala keeps this restriction (a class extends only one superclass) but mitigate the drawbacks by allowing the subclass to mix inas many traits it needs.

  • That's right: I'm talking about Scala classes and their incredible conciseness. It's impossible to look at a class declaration in Scala without appreciating the loathing Scala authors feel for verbosity. Not a single keystroke is wasted in declaring a class like:

    scala> class Greetings (var planet: String = "World") {
         |     def hello { println("Hello " + planet) }
         | }
    defined class Greetings

    scala> val world = new Greetings()
    world: Greetings = Greetings@311f56a3

    scala> val vulcan = new Greetings("Vulcan")
    vulcan: Greetings = Greetings@82f60ce

    scala> world.hello
    Hello World

    scala> vulcan.hello
    Hello Vulcan

  The Cog In The Machine On Which All Depends