Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Programming in Scala > Chapter 11: Scala's Hierarchy - Pg. 209

Chapter 11 Scala's Hierarchy Now that you've seen the details of class inheritance in the previous chapter, it is a good time to take a step back and look at Scala's class hierarchy as a whole. In Scala, every class inherits from a common superclass named Any . Because every class is a subclass of Any , the methods defined in Any are "universal" methods: they may be invoked on any object. Scala also defines some interesting classes at the bottom of the hierarchy, Null and Nothing , which essentially act as common subclasses. For example, just as Any is a superclass of every other class, Nothing is a subclass of every other class. In this chapter, we'll give you a tour of Scala's class hierarchy. 11.1 Scala's class hierarchy Figure 11.1 shows an outline of Scala's class hierarchy. At the top of the hierarchy is class Any , which defines methods that include the following: final def ==(that: Any): Boolean final def !=(that: Any): Boolean def equals(that: Any): Boolean def ##: Int def hashCode: Int def toString: String Because every class inherits from Any , every object in a Scala program can be compared using == , != , or equals ; hashed using ## or hashCode ; and formatted using toString . The equality and inequality methods, == and != , are declared final in class Any , so they cannot be overridden in subclasses. 209