This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
If object class is the Superclass of all Java classes.. then when we create reference variable of type object and assign to it an object of some class say xyz then why can't i access the methods of that class using that object?
I'm just a novice.. so an elaborate answer will be appreciated.
You can use the methods of the type that was used to declare the reference. You can think of a reference as working like a filter. It doesn't necessarily show you everything that object can do; it only shows you what the filter allows to pass through. In the case of Object, only the most basic facts about the object are allowed through the filter. In order to have access to more functionality, you have to declare your reference with a type that knows about that functionality.
Bert and Kathy have a good analogy in their Head First books. You can think of the reference as a remote control to a TV.
If you have a basicRemote, it has on, off, channel up/down, and volume up/down buttons, and that's all.
I could then go buy a super fancy hi-def TV. i could use my basicRemote to turn it on/off, change the channel, or change the volume. But since my remote does not have a picture-in-picture button, I can't use that feature on my TV. The feature certainly exists, but I have no way to get to it.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
See the rule is that if you use reference of superclass and the object of subclass then according the concept of polymorphism the methods that will be called will belong to subclass and the variables that will be called will be of superclass.