Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting between objects, does it retain anything in memory?

 
Caden Whitaker
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ahhh yeah, I should have known that!

Thanks!
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, of course, there is also the reflection solution.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic