Base Scala classes

And I’m back! Last month was really busy. Among other things, I had to travel, both for business and a small vacation. But now I’m back. Uhu! Thank you all for reading!

Now, back on topic. This time, I’ll cover the basic Scala classes on its class hierarchy. This is really simple stuff, but nice to know.

In Java, all the classes inherit from the class Object. And beside that we have primitive types. Now, in Scala, everything is an object. But to help things to be clearer, the top level classes are a few more then the sole Object class in Java. Take a look at the picture bellow:

Scala top level classes

Scala top level classes

The class Any is what everything else inherit from, directly or indirectly. Then we have Nothing, which represents, ahm… nothing. You can use it when you don’t want a method to return anything at all – we might come back to this in a later post, with examples. The two other interesting “sons” of Any are AnyVal and AnyRef. As their names help us understand, the former represents values that would be primitives in Java and the later represents object references.

An extra decendant of AnyVal is Unit. Its meaning is the same as void in Java – with this, you can explicitly return void from a method if you want, for example. On the AnyRef side, there is Null, which represents, ahm…. null. Questions? Pretty obvious, right?

One thing that I really like about this whole hierarchy is that it makes clear that in Scala EVERYTHING are objects. Beautiful, ain’t it??

Advertisement
This entry was posted in scala and tagged , , , , , . Bookmark the permalink.

1 Response to Base Scala classes

  1. Albert Kaneto says:

    Cool stuff man!
    I don’t know if it’s beautiful or not hehe, but certainly is clearer to me. Keep writing about Scala because i’m learning from your blog =P.

    Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s