Java. How to override method in class with private constructor










2















I want to override toString() method of class LocalDate. I know how to override toString() method of class with public constructor like java.util.Date using code like this:



Date date = new Date () 
@override
public String toString() ...



But i cant do the same thing with class that have private constructor like java.time.LocalDate
Is there are any way how i can workaround this?










share|improve this question

















  • 3





    No, you can’t do that. The LocalDate class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format a LocalDate into the string of your liking use an appropriate DateTimeFormatter.

    – Ole V.V.
    Nov 14 '18 at 14:55












  • Possibly related: Localdate.format, format is not applied

    – Ole V.V.
    Nov 14 '18 at 14:58











  • I appreciate the comeback!

    – GhostCat
    Nov 18 '18 at 18:19















2















I want to override toString() method of class LocalDate. I know how to override toString() method of class with public constructor like java.util.Date using code like this:



Date date = new Date () 
@override
public String toString() ...



But i cant do the same thing with class that have private constructor like java.time.LocalDate
Is there are any way how i can workaround this?










share|improve this question

















  • 3





    No, you can’t do that. The LocalDate class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format a LocalDate into the string of your liking use an appropriate DateTimeFormatter.

    – Ole V.V.
    Nov 14 '18 at 14:55












  • Possibly related: Localdate.format, format is not applied

    – Ole V.V.
    Nov 14 '18 at 14:58











  • I appreciate the comeback!

    – GhostCat
    Nov 18 '18 at 18:19













2












2








2


0






I want to override toString() method of class LocalDate. I know how to override toString() method of class with public constructor like java.util.Date using code like this:



Date date = new Date () 
@override
public String toString() ...



But i cant do the same thing with class that have private constructor like java.time.LocalDate
Is there are any way how i can workaround this?










share|improve this question














I want to override toString() method of class LocalDate. I know how to override toString() method of class with public constructor like java.util.Date using code like this:



Date date = new Date () 
@override
public String toString() ...



But i cant do the same thing with class that have private constructor like java.time.LocalDate
Is there are any way how i can workaround this?







java constructor override private tostring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:54









A_GubarA_Gubar

132




132







  • 3





    No, you can’t do that. The LocalDate class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format a LocalDate into the string of your liking use an appropriate DateTimeFormatter.

    – Ole V.V.
    Nov 14 '18 at 14:55












  • Possibly related: Localdate.format, format is not applied

    – Ole V.V.
    Nov 14 '18 at 14:58











  • I appreciate the comeback!

    – GhostCat
    Nov 18 '18 at 18:19












  • 3





    No, you can’t do that. The LocalDate class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format a LocalDate into the string of your liking use an appropriate DateTimeFormatter.

    – Ole V.V.
    Nov 14 '18 at 14:55












  • Possibly related: Localdate.format, format is not applied

    – Ole V.V.
    Nov 14 '18 at 14:58











  • I appreciate the comeback!

    – GhostCat
    Nov 18 '18 at 18:19







3




3





No, you can’t do that. The LocalDate class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format a LocalDate into the string of your liking use an appropriate DateTimeFormatter.

– Ole V.V.
Nov 14 '18 at 14:55






No, you can’t do that. The LocalDate class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format a LocalDate into the string of your liking use an appropriate DateTimeFormatter.

– Ole V.V.
Nov 14 '18 at 14:55














Possibly related: Localdate.format, format is not applied

– Ole V.V.
Nov 14 '18 at 14:58





Possibly related: Localdate.format, format is not applied

– Ole V.V.
Nov 14 '18 at 14:58













I appreciate the comeback!

– GhostCat
Nov 18 '18 at 18:19





I appreciate the comeback!

– GhostCat
Nov 18 '18 at 18:19












2 Answers
2






active

oldest

votes


















3














The simple answer is: you can't.



That class javadoc tells you:




public final class LocalDate




You can't extend a final class, therefore you can't change the behavior of any of its methods, at least not in Java. (Kotlin offers extension methods, that allow you to do "sort of" that thing).



But as pointed out in the comments, assuming that your problem is to properly format a Date, LocalDate, ... instance, then overriding toString() is simply the wrong approach. You turn date objects into formatted date strings by using a DateFormatter, see here for guidance.






share|improve this answer
































    0














    We cannot override methods of a final 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%2f53303002%2fjava-how-to-override-method-in-class-with-private-constructor%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









      3














      The simple answer is: you can't.



      That class javadoc tells you:




      public final class LocalDate




      You can't extend a final class, therefore you can't change the behavior of any of its methods, at least not in Java. (Kotlin offers extension methods, that allow you to do "sort of" that thing).



      But as pointed out in the comments, assuming that your problem is to properly format a Date, LocalDate, ... instance, then overriding toString() is simply the wrong approach. You turn date objects into formatted date strings by using a DateFormatter, see here for guidance.






      share|improve this answer





























        3














        The simple answer is: you can't.



        That class javadoc tells you:




        public final class LocalDate




        You can't extend a final class, therefore you can't change the behavior of any of its methods, at least not in Java. (Kotlin offers extension methods, that allow you to do "sort of" that thing).



        But as pointed out in the comments, assuming that your problem is to properly format a Date, LocalDate, ... instance, then overriding toString() is simply the wrong approach. You turn date objects into formatted date strings by using a DateFormatter, see here for guidance.






        share|improve this answer



























          3












          3








          3







          The simple answer is: you can't.



          That class javadoc tells you:




          public final class LocalDate




          You can't extend a final class, therefore you can't change the behavior of any of its methods, at least not in Java. (Kotlin offers extension methods, that allow you to do "sort of" that thing).



          But as pointed out in the comments, assuming that your problem is to properly format a Date, LocalDate, ... instance, then overriding toString() is simply the wrong approach. You turn date objects into formatted date strings by using a DateFormatter, see here for guidance.






          share|improve this answer















          The simple answer is: you can't.



          That class javadoc tells you:




          public final class LocalDate




          You can't extend a final class, therefore you can't change the behavior of any of its methods, at least not in Java. (Kotlin offers extension methods, that allow you to do "sort of" that thing).



          But as pointed out in the comments, assuming that your problem is to properly format a Date, LocalDate, ... instance, then overriding toString() is simply the wrong approach. You turn date objects into formatted date strings by using a DateFormatter, see here for guidance.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 15:16

























          answered Nov 14 '18 at 15:05









          GhostCatGhostCat

          91.9k1688147




          91.9k1688147























              0














              We cannot override methods of a final class.






              share|improve this answer





























                0














                We cannot override methods of a final class.






                share|improve this answer



























                  0












                  0








                  0







                  We cannot override methods of a final class.






                  share|improve this answer















                  We cannot override methods of a final class.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 15:42









                  Ole V.V.

                  29.2k63453




                  29.2k63453










                  answered Nov 14 '18 at 15:08









                  Karthik PKarthik P

                  336




                  336



























                      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%2f53303002%2fjava-how-to-override-method-in-class-with-private-constructor%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto