• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism problem

 
Shiveen Pandita
Greenhorn
Posts: 25
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic