• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How to cast an object as another type of object?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this project that asks me to return true if one object is the exact same as the other object. It also tells me to cast the "other" object as the first object. The first object is of type "GeoCountDownTimer" and the other object is of type "Object" other. How would I go about writing this method?

My code is as follow but I honestly have no idea how to solve it.



I do have this other static method that checks if two objects are equal to each other and works.
 
Rancher
Posts: 99
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically you need to override equals method. First check if the other object is actually of the type of GeoCountDownTimer, then perform a cast and compare fields year, month and day to see if objects are equal. Try on your own and post your code if you get stuck.
 
Steven Villarreal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After taking some of your advice, I wrote

then I attempted to cast it as

but got an error.
 
Steven Villarreal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I tired a little harder and got this

but for some odd reason the variables day and month and year cannot be found?
 
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cast other to GeoCountDownTimer oth. So, in the comparison, use
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop. You are getting nowhere fast. There are ways to write an equals() method and ways not to write an equals() method. Please look at the following three resources:-
  • Effective Java by Joshua Bloch (chapter 3)
  • Odersky Spoon and Venners
  • Angelika Langer
  • Even though some of those resources are old, the information they contain hasn't changed. Make sure to understand at least one of those articles before you write your equals() method.
     
    Sheriff
    Posts: 6163
    163
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I you're tempted to write
    Write this instead:
     
    and POOF! You're gone! But look, this tiny ad is still here:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!