Force subclass to override method with itself as parameter

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
9
down vote

favorite
1












I have an abstract Event class which has an abstract method:



abstract boolean intersect(Event object);


This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.



@Override
public boolean intersect(SubClassEvent e2)

boolean intersects = false;
if(this.weekDay == e2.weekDay)
if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
intersects = true;

else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
intersects = true;


return intersects;










share|improve this question



























    up vote
    9
    down vote

    favorite
    1












    I have an abstract Event class which has an abstract method:



    abstract boolean intersect(Event object);


    This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.



    @Override
    public boolean intersect(SubClassEvent e2)

    boolean intersects = false;
    if(this.weekDay == e2.weekDay)
    if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
    intersects = true;

    else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
    intersects = true;


    return intersects;










    share|improve this question

























      up vote
      9
      down vote

      favorite
      1









      up vote
      9
      down vote

      favorite
      1






      1





      I have an abstract Event class which has an abstract method:



      abstract boolean intersect(Event object);


      This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.



      @Override
      public boolean intersect(SubClassEvent e2)

      boolean intersects = false;
      if(this.weekDay == e2.weekDay)
      if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
      intersects = true;

      else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
      intersects = true;


      return intersects;










      share|improve this question















      I have an abstract Event class which has an abstract method:



      abstract boolean intersect(Event object);


      This method should check if two instances of an Event subclass intersect based on the instance variables of the particular subclass. I want to force any subclass of Event to override the method on its instance variables. What is the best way to design this? This is my current implementation, which is wrong since I am changing the parameter type. I have also tried using interfaces, but have run into similar problems with type parameters.



      @Override
      public boolean intersect(SubClassEvent e2)

      boolean intersects = false;
      if(this.weekDay == e2.weekDay)
      if (this.getStartTime() < e2.getStartTime() && this.getEndTime() > e2.getStartTime())
      intersects = true;

      else if(this.getStartTime() >= e2.getStartTime() && this.getStartTime() < e2.getEndTime())
      intersects = true;


      return intersects;







      java generics inheritance casting type-parameter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 15 mins ago









      Mark Rotteveel

      57.7k1473114




      57.7k1473114










      asked 4 hours ago









      Nicolae Stroncea

      10819




      10819






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          If you make the abstract class generic, you can achieve that:



          abstract class Event<T extends Event<T>> 
          abstract boolean intersect(T object);



          Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.



          class SubClassEvent extends Event<SubClassEvent> 
          @Override
          boolean intersect(SubClassEvent object)return true;






          share|improve this answer






















          • Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
            – Nicolae Stroncea
            4 hours ago










          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',
          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%2f53148650%2fforce-subclass-to-override-method-with-itself-as-parameter%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          10
          down vote



          accepted










          If you make the abstract class generic, you can achieve that:



          abstract class Event<T extends Event<T>> 
          abstract boolean intersect(T object);



          Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.



          class SubClassEvent extends Event<SubClassEvent> 
          @Override
          boolean intersect(SubClassEvent object)return true;






          share|improve this answer






















          • Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
            – Nicolae Stroncea
            4 hours ago














          up vote
          10
          down vote



          accepted










          If you make the abstract class generic, you can achieve that:



          abstract class Event<T extends Event<T>> 
          abstract boolean intersect(T object);



          Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.



          class SubClassEvent extends Event<SubClassEvent> 
          @Override
          boolean intersect(SubClassEvent object)return true;






          share|improve this answer






















          • Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
            – Nicolae Stroncea
            4 hours ago












          up vote
          10
          down vote



          accepted







          up vote
          10
          down vote



          accepted






          If you make the abstract class generic, you can achieve that:



          abstract class Event<T extends Event<T>> 
          abstract boolean intersect(T object);



          Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.



          class SubClassEvent extends Event<SubClassEvent> 
          @Override
          boolean intersect(SubClassEvent object)return true;






          share|improve this answer














          If you make the abstract class generic, you can achieve that:



          abstract class Event<T extends Event<T>> 
          abstract boolean intersect(T object);



          Subclasses will have to declare their own type as parameter. Unless your codebase uses raw types, this should work.



          class SubClassEvent extends Event<SubClassEvent> 
          @Override
          boolean intersect(SubClassEvent object)return true;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 26 mins ago

























          answered 4 hours ago









          ernest_k

          17.1k41535




          17.1k41535











          • Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
            – Nicolae Stroncea
            4 hours ago
















          • Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
            – Nicolae Stroncea
            4 hours ago















          Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
          – Nicolae Stroncea
          4 hours ago




          Thank you! It seemed I had some gaps with my understanding of type parameters, which your code helped clear up
          – Nicolae Stroncea
          4 hours ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148650%2fforce-subclass-to-override-method-with-itself-as-parameter%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery