0

I'm new to scala and working with GraphX api. Here I'm trying to create Graph using below code snippet

val graph = Graph(verticesRDD, edgesRDD).cache()

Graph is abstract class in org.apache.spark.graphx package, since its a abstract class we can not have its constructor.

Above code snippet must be calling Graph concrete class constructor. So In same abstract class, I found Graph object under which I found apply method and when I use like Graph.apply(verticesRDD, edgesRDD).cache(), its produce same output like Graph(verticesRDD, edgesRDD).cache()

So my doubts are, How come apply method in Graph object is related to Graph abstract class. I'm confused here, Can anyone help me to understand this ?

Which constructor of Graph abstract class is being called here ? Is apply method is being called as constructor or something else ?

Thanks

trying to understand

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
morgan
  • 29
  • 4
  • https://stackoverflow.com/questions/609744/what-is-the-rationale-behind-having-companion-objects-in-scala https://stackoverflow.com/questions/9806029/scala-companion-object-purpose https://stackoverflow.com/questions/36528736/case-class-and-companion-object https://stackoverflow.com/questions/75286750/class-companion-object-vs-case-class-itself/ – Dmytro Mitin Mar 13 '23 at 08:50
  • @DmytroMitin, companion object is a object of Anonymous class which is implementing abstract class and can have multiple constructors with diff name, Is it correct ? But in java we should have constructors with same name only..Makes sense ? – morgan Mar 13 '23 at 09:06
  • https://stackoverflow.com/questions/9737352/what-is-the-apply-function-in-scala – Dmytro Mitin Mar 13 '23 at 09:08
  • 1
    *"companion object is a object of Anonymous class"* No. A class and its companion object are two different classes. You can have as many methods as you want both in a class and in its companion (as many constructors as you want in the class). The `apply` method of companion is a factory method (using design-pattern terminology). – Dmytro Mitin Mar 13 '23 at 09:12
  • @DmytroMitin, but we cannot create constructors for abstract class, so whenever I'll try to create instance of abstract class, it will ultimately look to its companion object methods (constructor)..right ? If not, so you mean abstract class and its companion object not related to each other at all ? – morgan Mar 13 '23 at 09:18
  • Did you read the questions linked? – Dmytro Mitin Mar 13 '23 at 09:21
  • yes, but still confused – morgan Mar 13 '23 at 09:24
  • *"whenever I'll try to create instance of abstract class"* If the class is abstract you can't create its instance, only instances of its subclasses. *"it will ultimately look to its companion object methods (constructor)..right?"* No. *"abstract class and its companion object not related to each other at all"* Right. Just a class and its companion can see private members of each other (and also companion objects play special role in implicit resolution). – Dmytro Mitin Mar 13 '23 at 16:29
  • A regular class and its companion define 2 distinct runtime classes and 2 distinct types. Their relation is that they both can see each others private methods/fields/constructors etc BUT companion is a singleton in runtime and, for Java compatibility, its methods would become available as regular classes `static` methods for Java. Implicits containing class in their type will also be sought in classes companion. Companion will have an instance in runtime - normally from a different type hierarchy than an accompanied class. If that class is abstract... only its subtypes could have instances. – Mateusz Kubuszok Mar 13 '23 at 17:48

0 Answers0