• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recasting objects e.g. from clone to point

 
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?


 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you're doing with clone() I would do with a copy constructor. Then there is no casting.
 
reply
    Bookmark Topic Watch Topic
  • New Topic