Reputation: 27385
I'm very new to scala and now I have to work on a project which is written both in scala and java. I came across with this-like construction:
class SomeType{
//...
}
trait Trait1 extends scala.AnyRef{
}
trait Trait2 extends extends scala.AnyRef{
}
class MyClass(arg : SomeType) extends Trait1{
//...
}
object MyClass extends Trait2{
//...
}
It's kind of mind-numbing. As far as I got by reading this answer we can think of object
s as just classes with abstract methods. I.e. we could (In my opinion) define some mapping with Class<T>
object in java.
But in this example class
and object
extends different trait
s. And that's what I was really confused by. I cannot imagine what it means and why it was used.
Upvotes: 2
Views: 2168
Reputation: 1751
A class is just like any other class in other languages. You define class just like any other language with some syntax difference. Also, there are case classes which comes with some handy methods defined and allows for easy initialization of objects.
class Person(val name: String)
val me = new Person("My name")
However, object is a class with single object only. This makes it interesting as it can be used to create static members of a class using companion object. This companion object has access to private members of the class definition and it has the same name as the class you're defining.
class Person(var name: String) {
import Person._
def hi(): String = sayHello(name)
}
object Person {
private def sayHello(name: String): String = "Hello " + name
}
val me = new Person("My name")
me.hi()
Also, noteworthy point is that object class is lazily created which is another important point. So, these are not instantiated unless they are needed in our code.
If you're defining connection creation for JDBC, you can create them inside object to avoid duplication just like we do in Java with singleton objects.
Upvotes: 3
Reputation: 206796
I'll try to explain what a Scala object
is from the perspective of someone who knows Java.
In Java, you can define "normal" class members and static
class members. When something (for example a member variable) is static
in Java, then there's only one instance of that member variable, which is shared by all instances of the class.
In Scala, there is no static
. Compared to Java: whatever you would make static
in Java, you would put in an object
in Scala.
A Scala object
is a singleton - there's only a single instance of it, just like static
members in Java of which there's only a single instance.
I hope that makes it a little bit more clear what a Scala object
is exactly.
A Scala object
can extend classes or traits just like any other class or trait, so that it inherits whatever was defined in the class or trait that it extends. There's really no reason why this should not be possible.
Note that if you have a class
and an object
with the same name, then those two belong together - they can see each other's private members. The object
is called the companion object of the class.
Note that this does not mean that the object
is an instance of the class. It is not - it is a separate thing that stands beside the class. Maybe your confusion about the class and object extending different traits comes from a misunderstanding about that point.
Upvotes: 14