• 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

Converting between objects, does it retain anything in memory?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Standard User


Super Secret User



Main application


Ok... so that's a round about way of saing, if I return a type of "User" is it true in all circumnstances that any orphaned variables still remain somewhere attached to that object? Somewhere in memory perhaps? In my example, in main(), is it possible, somehow, somewhere, someway to determine jimmy's password just from examining the User object that was returned?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your getUser method creates a SecretUser instance. This will always be a SecretUser instance - you cannot convert it to another type.

You are referencing it with a User object reference ('user'). This means that (without any casting) you can only access the User specific parts of the instance. However, there is nothing to stop you referencing it with a SecretUser reference and then all parts of the instance will become asccessible.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The head first books explain casting pretty good. your object is like a tv, and your reference variable is like a remote.

so, you create a SecretUser object. That's like a TV that has picture-in-picture (PiP). you decide to connect it to a User reference - that's like a remote that doesn't have a button for PiP. The TV is still capable of doing it, you just don't have a button to turn it on.

Anyone else can get a remote that DOES have the PiP buttons, and if they use it, the PiP will come on.

A cast doesn't change the OBJECT, it only let's you connect a more or less 'sophisticated' reference to that object.
 
Caden Whitaker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh yeah, I should have known that!

Thanks!
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, of course, there is also the reflection solution.

 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic