Suppose I have the following java method
protected void onEvent(Object obj) { }
The Scala compiler accepts
protected override def onEvent(event: Any)
and
protected override def onEvent(event: Object)
Is there any difference between the two?
Scala Type Hierarchy Any is the supertype of all types, also called the top type. It defines certain universal methods such as equals , hashCode , and toString . Any has two direct subclasses: AnyVal and AnyRef . AnyVal represents value types.
In Scala, an object is a named instance with members such as fields and methods. An object and a class that have the same name and which are defined in the same source file are known as companions.
Difference Between Scala Classes and Objects Definition: A class is defined with the class keyword while an object is defined using the object keyword. Also, whereas a class can take parameters, an object can't take any parameter. Instantiation: To instantiate a regular class, we use the new keyword.
Everything is an Object. Scala is a pure object-oriented language in the sense that everything is an object, including numbers or functions. It differs from Java in that respect, since Java distinguishes primitive types (such as boolean and int ) from reference types.
There is an article on scala-lang with great diagram (I even put it on the wall). And also need to be mentioned:
If Scala is used in the context of a Java runtime environment, then scala.AnyRef corresponds to java.lang.Object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With