aspose file tools*
The moose likes Beginning Java and the fly likes Object has instance variables encapsulated by access methods... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object has instance variables encapsulated by access methods..." Watch "Object has instance variables encapsulated by access methods..." New topic
Author

Object has instance variables encapsulated by access methods...

Clovis Hartig II
Greenhorn

Joined: Sep 11, 2004
Posts: 20
Hi everyone,

I've been working my way through the wonderful Head First Java and able to understand it so far until I hit a . Could someone please shed light on the following paragraph from the lower left part of page 246?

Object has instance variables encapsulated by access methods. These instance variables are created when any subclass is instantiated. (These aren't the REAL Object variables, but we don't care what they are since they are encapsulated)


I understand that when a given object is created all the constructors in the classes above it are run. But I have at least 2 questions about the quote above:

1) what does "instance variables encapsulated by access methods" mean?
and
2) What are the "REAL Object variables" referred to above.

--Clueless on p. 246

Thanks very much in advance for your anticipated help.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30299
    
150

Clovis,
Suppose you have the following class:

And a caller:

1) The instance variable is myString. It has 2 methods that can be used to access it. From the point of view of the caller, myString isn't really a variable. It is something MyClass takes care of that is only used through the two methods.
2) The "REAL Object variables" is obj. It is what the caller sees as an object/variable.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Clovis Hartig II
Greenhorn

Joined: Sep 11, 2004
Posts: 20
Thanks Jeanne, that was very helpful! I understand most of it now but I'm afraid I'm still a tad shaky about why the instance variables created are not the "REAL" Object variables.

I see encapsulation means that the actual instance variable cannot be accessed (it's private) except through the getter and setter methods. But why are the instance aka object variable(s) created when a subclass constructor run different from the "Real" ones?

Thanks!
Albert Nunes
Greenhorn

Joined: Mar 22, 2004
Posts: 11
Hi,

1) Probably, it means that the instance variables have private modifiers and the methods that provide access to the instance variables are public. The only way to modify the instance variables is through the public access methods. That's what it means.

2) The instance variables does not belong to the subclass, it belongs to the superclass. The subclass inherits the access methods. So, once the variables are private, the only thing that is important is the access methods, not the variables itself, because you cannot access them directly.

I hope this could help.
Clovis Hartig II
Greenhorn

Joined: Sep 11, 2004
Posts: 20
Thanks Albert,

You took some things I did know and syntesized them into what I wasn't able to figure out. I'm pretty sure I "get it" now. I think I need to study all of this further too.

Thanks Jeanne and Albert, I'll continue on with chapter 9 in HF Java now...

After I finish this book I'll start plowing through all the free resources on Java that some great people on Javaranch have been listing and read my copy of "Just Java". I just bought it a month ago and now I see there's a second edition Oh well, I' sure it will still be quite useful. The capstone will be to study for cert. with the renowned SCJP cert book by Sierra and Bates.

Back to reading...
Peter van der Linden
author
Ranch Hand

Joined: Sep 28, 2004
Posts: 46
Hi Clovis,

I have a different perspective on the answer to your question, than those already expressed
here. And far be it from me to attempt to put words in anybody's mouth.

But (here goes) ... I think what Kathy and Bert are referring to when they say:
These instance variables are created when any subclass is instantiated. (These aren't the REAL Object variables ...

is that the class java.lang.Object contains some instance variables. Look it up! It really does. Nothing public however.

What Kathy and Bert are saying is that because we did a "new Snowboard()" we got a Snowboard
object. However, because Snowboard is a subclass of Object (everything is a subclass of Object),
Snowboard also has the instance variables declared in Object. These are not REAL Object
instance variables because they are brought to life by the creation of a Snowboard, not an
Object.

It's a throwaway remark, not a big deal. After all, since it is a subclass of Object, Snowboard
IS an Object, and thus would really have all the instance variables that Object has.

Hope this helps! Ignore if not!

Cheers,

Peter


Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0131482114/ref=jranch-20" target="_blank" rel="nofollow">Just Java(TM) 2 (6th Edition)</a>
Clovis Hartig II
Greenhorn

Joined: Sep 11, 2004
Posts: 20
Thanks Peter. Yes this helps; it's all good And I realize I have edition 5 of your book (not edition one as I mentioned before). I bought it about a month ago and will begin reading in ernest when I finish HF Java in the next week.

Wow, I sure have gotten some stellar help on my little 'ol question. thanks all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object has instance variables encapsulated by access methods...