Java. How to override method in class with private constructor
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
add a comment |
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
3
No, you can’t do that. TheLocalDate
class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format aLocalDate
into the string of your liking use an appropriateDateTimeFormatter
.
– 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
add a comment |
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
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
java constructor override private tostring
asked Nov 14 '18 at 14:54
A_GubarA_Gubar
132
132
3
No, you can’t do that. TheLocalDate
class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format aLocalDate
into the string of your liking use an appropriateDateTimeFormatter
.
– 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
add a comment |
3
No, you can’t do that. TheLocalDate
class is final, meaning you cannot create any subclass. It’s also the wrong way of obtaining what you want. To format aLocalDate
into the string of your liking use an appropriateDateTimeFormatter
.
– 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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
We cannot override methods of a final class.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 14 '18 at 15:16
answered Nov 14 '18 at 15:05
GhostCatGhostCat
91.9k1688147
91.9k1688147
add a comment |
add a comment |
We cannot override methods of a final class.
add a comment |
We cannot override methods of a final class.
add a comment |
We cannot override methods of a final class.
We cannot override methods of a final class.
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 aLocalDate
into the string of your liking use an appropriateDateTimeFormatter
.– 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