Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 1050
27
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: 7113
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.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic