Scala difference between object and class - Stack Overflow
A class
is a definition, a description. It defines a type in terms of methods and composition of other types.
An object
is a singleton -- an instance of a class which is guaranteed to be unique. For every object
in the code, an anonymous class is created, which inherits from whatever classes you declared object
to implement. This class cannot be seen from Scala source code -- though you can get at it through reflection.
There is a relationship between object
and class
. An object is said to be the companion-object of a class if they share the same name. When this happens, each has access to methods of private
visibility in the other. These methods are not automatically imported, though. You either have to import them explicitly, or prefix them with the class/object name.
For example:
class X { // Prefix to call def m(x: Int) = X.f(x) // Import and use import X._ def n(x: Int) = f(x) } object X { def f(x: Int) = x * x }
Read full article from Scala difference between object and class - Stack Overflow
No comments:
Post a Comment