Programming in Scala Gist 11

Vinu Charanya
vTechNotes
Published in
3 min readMay 29, 2016

These are the key points from the Programming in Scala, Second Edition by Martin Odersky, Lex Spoon, Bill Venners. I wanted to make note of some key points. They may seem broken and not make much sense, if you have not read the book. This is for personal reference. Feel free to correct me if I have interpreted anything wrong in this post.

Chapter 11— Scala’s Hierarchy

In Scala, every class inherits from a common superclass named Any. Methods defined in Any are “universal” methods and may be invoked on any object. At the end of the hierarchy, Scala defines Null and Nothing and are common subclasses.

11.1 Scala’s class hierarchy

Some of the methods defined in Any that is available for all Scala objects
  • Root class Any has two subclasses: AnyVal and AnyRef. AnyVal is the parent class of every built-in value class in Scala.
  • Nine value classes: Byte, Short, Char, Int, Long, Float, Double, Boolean and Unit. Value class space is flat and they have implicit conversion (For Eg., To convert Int to Long)
  • The first eight of these correspond to Java’s primitive types, and their values are represented at run time as Java’s primitive values.
  • The instances of these classes are all written as literals in Scala and are all defined to be both abstract and final.
  • Unit, corresponds roughly to Java’s void type; it is used as the result type of a method that does not otherwise return an interesting result. Unit has a single instance value, which is written ().
  • AnyRef is the base class for all reference classes in Scala. AnyRef is just an alias for java.lang.Object.
  • Scala classes are different from Java classes as they also inherit from a special marker trait called ScalaObject

11.2 How primitives are implemented

  • Scala stores integers in the same way as Java: as 32-bit words. Standard operations like addition or multiplication are implemented as primitive operations.
  • Integers of type Int are converted transparently to “boxed integers” of java.lang.Integer whenever necessary.
  • Scala == methods checks for values and eq method checks for reference equality as well.

11.3 Bottom types

  • scala.Null and scala.Nothing are special types that handle some corner cases of Scala’s object-oriented type system in a uniform way.
  • Class Null is the type of the null reference and it is a subclass of every reference class. Null is not compatible with value types.
  • Type Nothing is at the very bottom of Scala’s class hierarchy; it is a sub-type of every other type. one use of Nothing is that it signals abnormal termination.

Conclusion

The above gist lays a good foundation on class inheritance in Scala.

Previous:

Next:

--

--