Class and Companion Objects hierarchy

41 views
Skip to first unread message

pedro rijo

unread,
Feb 25, 2015, 1:30:22 PM2/25/15
to scala...@googlegroups.com
Hey,

I would like to better understand how does class inheritance works when mixing classes and companion objects.

If a class Child extends Parent (possibly with ParentTrait), the Object Child will inherit from Parent (and ParentTrait) also right?

But can Object Child extends other classes/traits? If so, what will be the implications for the Child class, and for the Child companion object?

Thanks,
Pedro Rijo

Naftoli Gugenheim

unread,
Feb 25, 2015, 3:54:55 PM2/25/15
to pedro rijo, scala...@googlegroups.com
On Wed Feb 25 2015 at 1:30:20 PM pedro rijo <pedro...@gmail.com> wrote:
Hey,

I would like to better understand how does class inheritance works when mixing classes and companion objects.

If a class Child extends Parent (possibly with ParentTrait), the Object Child will inherit from Parent (and ParentTrait) also right?

No.
 

But can Object Child extends other classes/traits? If so, what will be the implications for the Child class, and for the Child companion object?

They are two unrelated types. The object can extend whatever it wants, and the class can extend whatever it wants.


Thanks,
Pedro Rijo

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pedro rijo

unread,
Feb 25, 2015, 3:59:02 PM2/25/15
to scala...@googlegroups.com, pedro...@gmail.com
So what you are saying is that are 2 independent objects, the only diference is that Object can access Class instance private variables?

Naftoli Gugenheim

unread,
Feb 25, 2015, 4:00:51 PM2/25/15
to pedro rijo, scala...@googlegroups.com
Basically, it could be there are other ramifications that are not coming to mind.

Oliver Ruebenacker

unread,
Feb 25, 2015, 4:06:12 PM2/25/15
to pedro rijo, scala-user

     Hello,

  If you have a case class, the compiler will add an apply method to its companion object that takes the same arguments as the primary constructor of the case class, passes them to the primary constructor, and returns the constructed instance. If you did not declare a companion object, the compiler will create one.

  If you access Scala code from Java code, a class and its companion object will look like the same class on the Java side, with the members of the companion object being the static members.

     Best, Oliver 
--
Oliver Ruebenacker
Solutions Architect at Altisource Labs
Be always grateful, but never satisfied.

pedro rijo

unread,
Feb 25, 2015, 4:09:22 PM2/25/15
to Oliver Ruebenacker, scala-user
Thank you all :)
--
Obrigado,

Pedro Rijo

Naftoli Gugenheim

unread,
Feb 25, 2015, 4:21:33 PM2/25/15
to pedro rijo, scala...@googlegroups.com
On Wed Feb 25 2015 at 4:00:47 PM Naftoli Gugenheim <nafto...@gmail.com> wrote:
Basically, it could be there are other ramifications that are not coming to mind.

Right --- implicit search.

class C
object D {
  implicit val c: C = new C
}

implicitly[C] // compiler error --- but if you rename D to C then it finds it.


 

To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages