3

Diamond problem is handled in some OOPS languages (eg. curl) by having the repeatedly inherited class as "shared"? I want to know how this works. Also, I want to know the role played by primary and secondary constructors in solving the diamond problem in these OOPS languages when shared strategy is used. Suppose there are 4 classes say A,B,C and D. Let the inheritance structure is B and C inherit A and D inherits both B and C. Each class has a variable say A has a, B has b, C has c and D has d. How does the object creation happens for each class?

  • why is this tagged with `curl` ? – hanshenrik Nov 14 '18 at 00:28
  • 1
    this is the format in the oops language curl – Kamalapriya Subramanian Nov 14 '18 at 02:26
  • my concern is not about curl. I want to know how "shared" strategy solves diamond problem? – Kamalapriya Subramanian Nov 14 '18 at 02:33
  • 1
    @user1067003 - this is the format in the oops language curl. my concern is not about curl. I want to know how "shared" strategy solves diamond problem? – Kamalapriya Subramanian Nov 14 '18 at 02:39
  • @hanshenrik this probably was tagged with `curl` because either the OP referred the Wikpedia [here](https://en.wikipedia.org/wiki/Diamond_problem#Mitigation) or the Wikipedia authors are referring to the same source (or a reference to it) as the OP is. – progmatico Nov 16 '18 at 16:54
  • @KamalapriyaSubramanian you don't mention what language you want the diamond problem mitigation explained. What happens to your objects is different from language to language, as you can read in the Wikipedia page. Maybe it was for commenting `Curl`after all. And not `curl' that is a tag used for a command-line tool. – progmatico Nov 16 '18 at 16:56
  • @progmatico - As you said i refered to wikipedia pages only. But i dont understand it properly. I havent worked in curl before so no idea about curl but just i want to know how that language handles the "diamond problem" using "shared" strategy and also using primary and secondary constructor concepts? I want to know how child objects (multiple inheritance) inherit the grandparent's attributes (suppose D inherits classes B and C both of which inherit class A. Here D is child and A is grandparent) and how they react when primary and secondary constructors are invoked? – Kamalapriya Subramanian Nov 17 '18 at 00:33
  • @KamalapriyaSubramanian can't find docs for Curl. But maybe Kotlin docs helps in that matter. Try [Kotlin docs](https://kotlinlang.org/docs/reference/classes.html) and this SO questions [here](https://stackoverflow.com/questions/44843741/kotlin-constructors-primary-and-secondary) and [here](https://stackoverflow.com/questions/51541913/why-does-kotlin-have-two-types-of-constructors). – progmatico Nov 17 '18 at 23:38
  • It looks like there are some assumptions about what constructors will run in a class, and if you don't run a primary constructor, you delegate to a secondary, or to the base class. Primaries are like a default constructor while secondaries cover variations in constructor arguments. So what attributes get created depend on what you do in these constructors code. Maybe the shared term has to do with these kind of collaboration in derived classes, of delegating, invoking or overriding other constructors, including base ones, not sure. – progmatico Nov 17 '18 at 23:38
  • @progmatico - Thank you so much for your replies.... Even i didnt find any material online regarding curl. That's why posted this question. Even i read through some materials on Kotlin but as you said im not getting sure and convincing understanding about how shared strategy helps avoiding diamond problem. Also the role of primary and secondary constructors in avoiding the occurence of diamond problem. Im trying to make my own assumptions and trying to understand them in that framework but nothing is sure whether my interpretations are correct or not. – Kamalapriya Subramanian Nov 18 '18 at 18:09
  • I edited my answer. Note that I am not certain of the answer, it just seems the most logical for me, without access to proper documentation. – progmatico Nov 19 '18 at 10:44

1 Answers1

1

Citing Wikipedia at https://en.wikipedia.org/wiki/Multiple_inheritance at the Curl bullet:

Curl allows only classes that are explicitly marked as shared to be inherited repeatedly. Shared classes must define a secondary constructor for each regular constructor in the class. The regular constructor is called the first time the state for the shared class is initialized through a subclass constructor, and the secondary constructor will be invoked for all other subclasses.

From here, without knowing Curl and only with the quote above and this, where it is stated that

The object semantics of Curl are similar to those of Java and C++.

Given

    A
   /  \
 B(A) C(A)
   \   /
   D(B,C)

I imagine (I don't know for sure) that the coder is responsible to disambiguate the problem by specifying the qualified name of the constructor to run, when invoking a parent constructor from the D(B,C) subclass.

It looks like A has to be declared shared, and when D is created, B runs a constructor that calls A (primary) constructor, C runs a constructor that calls A (secondary) constructor. The distinction between primary/secondary constructor call is automatic and transparent to the coder.

As two A constructors are invoked, two A objects are created in memory, that is the A class is shared with two different subclasses, but there is not a single "shared" A object, but two independent ones (see also virtual/nonvirtual inheritance that is somehow related (C++).)

For what I've read for several different languages, it is almost always the coder that disambiguates the diamond problem with qualification. Languages just define different or similar schemes of giving an error, or having a criteria to choose one of the multiple ambiguous definitions, like specific search order in the inheritance chain. Some other languages don't even allow multiple inheritance, but in some of these you are allowed to extend functionality by some ohter means (like interfaces).

progmatico
  • 4,714
  • 1
  • 16
  • 27
  • So there is no single shared A's object when instantiating through class D and might be this strategy is same as that of non-virtual strategy of C++. Thank you for your explanation. Atleast now i have some idea about this strategy. – Kamalapriya Subramanian Nov 19 '18 at 22:42