aspose file tools*
The moose likes Beginning Java and the fly likes Polymorphism problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism problem" Watch "Polymorphism problem" New topic
Author

Polymorphism problem

Shiveen Pandita
Greenhorn

Joined: Jul 30, 2012
Posts: 25

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.

Thanks in advance
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.

Hope that helps.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Shiveen Pandita wrote:I'm just a novice.. so an elaborate answer will be appreciated.

In our experience, if you're a novice, the simpler the answer the better. It also helps if you actually work out as much as you can for yourself; and I think Dennis has given you plenty to work with.

BTW - A tip from an old hand: It's 'Object'; not 'object'. It may seem small to you, but Java is case-sensitive, and the compiler DOES NOT recognise spelling mistakes.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

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
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Suppose you have a Class A, and its subclass B...
Now you create a reference of type A, and assign it to object of type B...

A obj = new B();

Now, with this reference obj, you can access all the methods of Class B (provided the method is defined in Class A)

Similar is the case with Object Class.... with a reference of type Object, pointing to any object, you can access only those methods defined in Object Class..

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

BTW Fred, That was a super example... I love it ... I think my answer is not needed if one reads yours... Didn't read it before...
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism problem