Using alternative super class constructor in child class instantiation





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a base class with two constructors, and a child class which has one constructor. Is it possible to instantiate a child class using the second base class constructor?



Example code:



abstract class RuleCondition(rule:Rule, field:String, equal:Boolean, inverted:Boolean)
{
// alternate constructor with RuleValue instead of static comparation value

def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = ???
}

class RuleConditionAbove(rule:Rule, field:String, comparationValue:Long, equal:Boolean = false, inverted:Boolean = false)
extends RuleCondition(rule, field, equal, inverted)
{
// ...
}


Now I can do this:



val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


but I cannot do this:



val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


because the alternative constructor of RuleCondition base class is not visible. It will be visible once I add this to the child class:



def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


Would this be the only/usual way of solving this issue, or is there something smarter which involves less copy & past code? (since I have a ton of child classes of the same pattern)



[edit] To clarify, the second constructor would be the same in every child class, thus I would like to have it implemented only once in the base class.
However still having to put another constructor in each child class would defeat this purpose somehow, and thus I would not have two constructors in the base class but rather only in all child classes.










share|improve this question































    1















    I have a base class with two constructors, and a child class which has one constructor. Is it possible to instantiate a child class using the second base class constructor?



    Example code:



    abstract class RuleCondition(rule:Rule, field:String, equal:Boolean, inverted:Boolean)
    {
    // alternate constructor with RuleValue instead of static comparation value

    def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = ???
    }

    class RuleConditionAbove(rule:Rule, field:String, comparationValue:Long, equal:Boolean = false, inverted:Boolean = false)
    extends RuleCondition(rule, field, equal, inverted)
    {
    // ...
    }


    Now I can do this:



    val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


    but I cannot do this:



    val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


    because the alternative constructor of RuleCondition base class is not visible. It will be visible once I add this to the child class:



    def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


    Would this be the only/usual way of solving this issue, or is there something smarter which involves less copy & past code? (since I have a ton of child classes of the same pattern)



    [edit] To clarify, the second constructor would be the same in every child class, thus I would like to have it implemented only once in the base class.
    However still having to put another constructor in each child class would defeat this purpose somehow, and thus I would not have two constructors in the base class but rather only in all child classes.










    share|improve this question



























      1












      1








      1








      I have a base class with two constructors, and a child class which has one constructor. Is it possible to instantiate a child class using the second base class constructor?



      Example code:



      abstract class RuleCondition(rule:Rule, field:String, equal:Boolean, inverted:Boolean)
      {
      // alternate constructor with RuleValue instead of static comparation value

      def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = ???
      }

      class RuleConditionAbove(rule:Rule, field:String, comparationValue:Long, equal:Boolean = false, inverted:Boolean = false)
      extends RuleCondition(rule, field, equal, inverted)
      {
      // ...
      }


      Now I can do this:



      val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


      but I cannot do this:



      val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


      because the alternative constructor of RuleCondition base class is not visible. It will be visible once I add this to the child class:



      def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


      Would this be the only/usual way of solving this issue, or is there something smarter which involves less copy & past code? (since I have a ton of child classes of the same pattern)



      [edit] To clarify, the second constructor would be the same in every child class, thus I would like to have it implemented only once in the base class.
      However still having to put another constructor in each child class would defeat this purpose somehow, and thus I would not have two constructors in the base class but rather only in all child classes.










      share|improve this question
















      I have a base class with two constructors, and a child class which has one constructor. Is it possible to instantiate a child class using the second base class constructor?



      Example code:



      abstract class RuleCondition(rule:Rule, field:String, equal:Boolean, inverted:Boolean)
      {
      // alternate constructor with RuleValue instead of static comparation value

      def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = ???
      }

      class RuleConditionAbove(rule:Rule, field:String, comparationValue:Long, equal:Boolean = false, inverted:Boolean = false)
      extends RuleCondition(rule, field, equal, inverted)
      {
      // ...
      }


      Now I can do this:



      val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


      but I cannot do this:



      val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


      because the alternative constructor of RuleCondition base class is not visible. It will be visible once I add this to the child class:



      def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


      Would this be the only/usual way of solving this issue, or is there something smarter which involves less copy & past code? (since I have a ton of child classes of the same pattern)



      [edit] To clarify, the second constructor would be the same in every child class, thus I would like to have it implemented only once in the base class.
      However still having to put another constructor in each child class would defeat this purpose somehow, and thus I would not have two constructors in the base class but rather only in all child classes.







      scala inheritance constructor polymorphism superclass






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 11:23









      Jeffrey Chung

      14.6k62242




      14.6k62242










      asked Jan 3 at 8:13









      user826955user826955

      1,24711740




      1,24711740
























          2 Answers
          2






          active

          oldest

          votes


















          4















          Is it possible to instanciate a child class using the [second] base class constructor?




          No.



          You can never use a superclass constructor to create an instance of a subclass. You have to call a constructor for the class that you are creating. The subclass constructor must call a constructor for the superclass, but you can't call it directly.



          So the reason that you can do this



          val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


          is that RuleConditionAbove has a constructor with those arguments. It has nothing to do with the fact that RuleCondition has a constructor with the same arguments.



          And the reason that you can't do this



          val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


          is that RuleConditionAbove does not have a constructor with those arguments.






          share|improve this answer































            0














            You have to add a constructor definition in every child class as you described.



            def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


            Imagine that a child class defines new fields that are not available in the base class. Creating child class with a base constructor would not define such fields and leave the instance of the class partially initialised.



            If your base constructor has valuable logic, it makes sense to keep it in the base class. And just "link" it to the base constructor in the child class.






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018525%2fusing-alternative-super-class-constructor-in-child-class-instantiation%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4















              Is it possible to instanciate a child class using the [second] base class constructor?




              No.



              You can never use a superclass constructor to create an instance of a subclass. You have to call a constructor for the class that you are creating. The subclass constructor must call a constructor for the superclass, but you can't call it directly.



              So the reason that you can do this



              val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


              is that RuleConditionAbove has a constructor with those arguments. It has nothing to do with the fact that RuleCondition has a constructor with the same arguments.



              And the reason that you can't do this



              val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


              is that RuleConditionAbove does not have a constructor with those arguments.






              share|improve this answer




























                4















                Is it possible to instanciate a child class using the [second] base class constructor?




                No.



                You can never use a superclass constructor to create an instance of a subclass. You have to call a constructor for the class that you are creating. The subclass constructor must call a constructor for the superclass, but you can't call it directly.



                So the reason that you can do this



                val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


                is that RuleConditionAbove has a constructor with those arguments. It has nothing to do with the fact that RuleCondition has a constructor with the same arguments.



                And the reason that you can't do this



                val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


                is that RuleConditionAbove does not have a constructor with those arguments.






                share|improve this answer


























                  4












                  4








                  4








                  Is it possible to instanciate a child class using the [second] base class constructor?




                  No.



                  You can never use a superclass constructor to create an instance of a subclass. You have to call a constructor for the class that you are creating. The subclass constructor must call a constructor for the superclass, but you can't call it directly.



                  So the reason that you can do this



                  val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


                  is that RuleConditionAbove has a constructor with those arguments. It has nothing to do with the fact that RuleCondition has a constructor with the same arguments.



                  And the reason that you can't do this



                  val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


                  is that RuleConditionAbove does not have a constructor with those arguments.






                  share|improve this answer














                  Is it possible to instanciate a child class using the [second] base class constructor?




                  No.



                  You can never use a superclass constructor to create an instance of a subclass. You have to call a constructor for the class that you are creating. The subclass constructor must call a constructor for the superclass, but you can't call it directly.



                  So the reason that you can do this



                  val myAboveCondition = new RuleConditionAbove(rule, "bla", 10, true, false)


                  is that RuleConditionAbove has a constructor with those arguments. It has nothing to do with the fact that RuleCondition has a constructor with the same arguments.



                  And the reason that you can't do this



                  val myAboveCondition = new RuleConditionAbove(rule, "bla", RuleValue(...), true, false)


                  is that RuleConditionAbove does not have a constructor with those arguments.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 8:49









                  TimTim

                  6,8071819




                  6,8071819

























                      0














                      You have to add a constructor definition in every child class as you described.



                      def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


                      Imagine that a child class defines new fields that are not available in the base class. Creating child class with a base constructor would not define such fields and leave the instance of the class partially initialised.



                      If your base constructor has valuable logic, it makes sense to keep it in the base class. And just "link" it to the base constructor in the child class.






                      share|improve this answer




























                        0














                        You have to add a constructor definition in every child class as you described.



                        def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


                        Imagine that a child class defines new fields that are not available in the base class. Creating child class with a base constructor would not define such fields and leave the instance of the class partially initialised.



                        If your base constructor has valuable logic, it makes sense to keep it in the base class. And just "link" it to the base constructor in the child class.






                        share|improve this answer


























                          0












                          0








                          0







                          You have to add a constructor definition in every child class as you described.



                          def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


                          Imagine that a child class defines new fields that are not available in the base class. Creating child class with a base constructor would not define such fields and leave the instance of the class partially initialised.



                          If your base constructor has valuable logic, it makes sense to keep it in the base class. And just "link" it to the base constructor in the child class.






                          share|improve this answer













                          You have to add a constructor definition in every child class as you described.



                          def this(rule:Rule, field:String, ref:RuleValue, equal:Boolean = false, inverted:Boolean = false) = this(rule, field, ref, equal, inverted)


                          Imagine that a child class defines new fields that are not available in the base class. Creating child class with a base constructor would not define such fields and leave the instance of the class partially initialised.



                          If your base constructor has valuable logic, it makes sense to keep it in the base class. And just "link" it to the base constructor in the child class.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 3 at 8:52









                          Ivan StanislavciucIvan Stanislavciuc

                          1,670411




                          1,670411






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018525%2fusing-alternative-super-class-constructor-in-child-class-instantiation%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                              SQL update select statement

                              'app-layout' is not a known element: how to share Component with different Modules