Scala's class hierarchy: Similarities and differences with Java classes

Source: Internet
Author: User
Tags abstract final

Emoticons 11.1 Scala class level diagram

Emoticons 11.1 shows the Scala class-level outline. The top of the hierarchy is the class any, which defines the methods that include the following:

Final def = = (That:any): Boolean
final def!= (That:any): Boolean
def equals (That:any): Boolean
def has Hcode:int
def tostring:string

Because each class inherits from the Any,scala program, each object can be compared with ==,!= or equals, with a hashcode hash, and with ToString format. The Equals and!= in the class any, = = and the same, are declared final, so they cannot be overloaded within subclasses. In fact, = = always the same as equals,!= is always the opposite of equals. Therefore, a separate class can trim the meaning of = = or!= by overloading the Equals method. We'll show you an example later in this chapter.

The root class any has two subclasses: Anyval and Anyref. Anyval is the parent class of each built-in value class in Scala. There are nine such value classes: Byte,short, Char,int,long,float,double,boolean and unit. The first eight of these correspond to the original Java type, whose values are represented at runtime as the original Java values. In Scala, instances of these classes are written in text. For example, 42 is an instance of int, ' X ' is an instance of char, and false is an instance of Boolean. You cannot use new to create instances of these classes. This is "small trick", the value class are defined as that is the abstract and final, mandatory implementation. So if you have written:

Scala> New Int

You will get:

< Console>:5:error:class Int is abstract; Cannot be

instantiated
new Int
ˆ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.