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.
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.
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?
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
Joined: Sep 11, 2004
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.
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!
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
Joined: Sep 11, 2004
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!