• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

polymorphism

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When looking at the code below why wouldn't the call to b.size return the Runtime size value? I kind of expected it too for the same reason that you get Runtime's method called. Does polymorphism only work with methods? And what is the logic with that?
1. public class Base {
2. protected int size = 100;
3. public int getSize() {
4. return size;
5. }
6. }
1. public class Runtime extends Base {
2. protected int size = 10;
3. public int getSize(){
4. return size;
5. }
6. public static void main(String[] args) {
7. Base b = new Runtime();
8. System.out.println (b.size + "," + b.getSize());
9. }
10. }
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by E Weibust:
When looking at the code below why wouldn't the call to b.size return the Runtime size value? ...
Does polymorphism only work with methods?
And what is the logic with that?


Yes, polymorphism only works on methods. Since b is of the superclass type and you are directly accessing a field, there is no point for polymorphic behavior to occur. I'd actually find it disconcerting if I tried to access a field in an object and got a subclass value for that field. Use polymorphism to encapsulate the differences in behavior.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you're right. Only non-static methods are overridden. That's it. Fields of the same name are hidden by the subclass as are static methods with the same argument signature.
The reference type determins which field you are going to access. So you look and see that your reference type is Base, so when resolving name, go to the base class.
You could do this to get Runtime's name, or just cast it to a variable of type Runtime.

[ June 17, 2003: Message edited by: Brian Joseph ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by E Weibust:
When looking at the code below why wouldn't the call to b.size return the Runtime size value?


Yes, accessing inherited methods & fields varies in this respect. I found it counter-intuitive the first time i came across it.
From "The Java Programming Language 3rd Edition" Section 3.3.3 Accessing Inherited Members:
When you invoke a method through an object reference, the actual class of the object governs which implementation is used. When you access a field, the declared type of the reference is used.
(italics as in the book)
 
E Weibust
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great answers everyone.
Thanks...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic