What use could someone have to override this (getClass()) method? What purpose could it solve practically.
The java documentation says this: By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
Why would this be true? What does this truth hold? Where is the information being stored that knows what type of object it is? How does this work?
Sorry this question I deleted and I shouldn't have. I meant to edit it. Won't happen again.
EDIT: I misread the documentation, it is final X-(, but I still would like to ask the second question
The getClass
method is public final
. It can not be overridden.
Using clone
is definitely not a good practice.
I think what the java documentation is saying is don't change the implementation of clone. If you override clone, use super.clone
to keep the same implementation.
If you don't change the implementation, what is the point of overriding? You could add logging. You could loosen it from protected
to public
. etc
If you don't change the implementation, then it will have the standard implementation which evidently guarantees that property. On the other hand, if you change the implementation, all bets are off.
Where is Object reflective information stored?
In the class.
What use could someone have to override this (getClass()) method? What purpose could it solve practically.
No purpose whatsoever, because getClass()
is final, so it cannot be overridden, so your question isn't about anything that can actually occur.
Why would this be true?
Because Object.getClass()
returns the class of the Object
it is called on, and because Object.clone()
returns an object of the same class that it was called on, and because a chain of super.clone()
calls will terminate at Object.clone()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.