Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - new vs object extends

Tags:

scala

What is the difference between defining an object using the new operator vs defining a standalone object by extending the class?

More specifically, given the type class GenericType { ... }, what is the difference between val a = new GenericType and object a extends GenericType?

like image 246
yatin Avatar asked Apr 24 '13 03:04

yatin


People also ask

Can object be extended in Scala?

Classes, case classes, objects, and (yes) traits can all extend no more than one class but can extend multiple traits at the same time. Unlike the other types, however, traits cannot be instantiated. Traits look about the same as any other type of class. However, like objects, they cannot take class parameters.

What is the difference between extends and with in Scala?

The first thing you inherit from can either be a trait or a class, using the extends keyword. You can define further inherited traits (and only traits) using the with keyword.

What does Extends mean in Scala?

Extending a class in Scala user can design an inherited class. To extend a class in Scala we use extends keyword. there are two restrictions to extend a class in Scala : To override method in scala override keyword is required. Only the primary constructor can pass parameters to the base constructor.

What is the difference between Scala class and object?

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.


2 Answers

As a practical matter, object declarations are initialized with the same mechanism as new in the bytecode. However, there are quite a few differences:

  • object as singletons -- each belongs to a class of which only one instance exists;
  • object is lazily initialized -- they'll only be created/initialized when first referred to;
  • an object and a class (or trait) of the same name are companions;
  • methods defined on object generate static forwarders on the companion class;
  • members of the object can access private members of the companion class;
  • when searching for implicits, companion objects of relevant* classes or traits are looked into.

These are just some of the differences that I can think of right of the bat. There are probably others.

* What are the "relevant" classes or traits is a longer story -- look up questions on Stack Overflow that explain it if you are interested. Look at the wiki for the scala tag if you have trouble finding them.

like image 153
Daniel C. Sobral Avatar answered Sep 29 '22 11:09

Daniel C. Sobral


object definition (whether it extends something or not) means singleton object creation.

scala> class GenericType
defined class GenericType

scala> val a = new GenericType
a: GenericType = GenericType@2d581156

scala> val a = new GenericType
a: GenericType = GenericType@71e7c512

scala> object genericObject extends GenericType
defined module genericObject

scala> val a = genericObject
a: genericObject.type = genericObject$@5549fe36

scala> val a = genericObject
a: genericObject.type = genericObject$@5549fe36
like image 31
Kazuhiro Sera Avatar answered Sep 29 '22 12:09

Kazuhiro Sera