Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Recasting objects e.g. from clone to point  RSS feed

 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I was trying to understand how to clone() objects e.g. a point in a cartesian pane, and how to recast them from type "clone" to type "point".
I have a question about the general use and applicability of "recast".
Here are the Java classes, first I created an abstract class for re-use by all sorts of shapes downstream:




... then I created a Point class inheriting from the abstracted super class:




QUESTION: going forward, is this how I can always recast one object to another e.g. from a cloned object to a point on an x-y pane, a circle etc ?


 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never seen where an object is copied as a Class of type clone before. I've seen clone as a method in many classes and have implemented clone() in several of my own, clone() usually refers to making an exact copy of the object you currently have, so setting any properties as the object values you are cloning. If I have a Point (X, Y) where X=128 and Y=200, my clone() of my Point object would give me a new Point (X, Y) where X=128 and Y=200.

This may give you an interesting read: https://en.wikipedia.org/wiki/Clone_(Java_method)

You're method will not always work as described in the article, and to me places unneeded steps in the process, unless you are looking to make a "Clone Factory" of sorts.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're doing with clone() I would do with a copy constructor. Then there is no casting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!