• 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 help...

 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little unclear on the behavior of the following code:

Here is the result:

I kind of expected 37 instead of 42...I guess the dependence here is on which things the compiler decides ahead of time and what is decided at run-time...and I understand the polymorphism comes into play when the correct method for that type of object is chosen at runtime, but what happened when choosing the appropriate field?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephen,
Value of the field depends on the declared type of the variable, since a is declared as Animal, a.size=42.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephen -
A good way to think about polymorphism is that it only applies to 'instance methods'.
-Bert
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephen -
A good way to think about polymorphism is that it only applies to 'instance methods'.
-Bert
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about the fact that class dog is method-local class, so therefore it cannot access any variables inside its class that are not marked final!!
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What about the fact that class dog is method-local class, so therefore it cannot access any variables inside its class that are not marked final!!


A method-local class can not access any vars in the method, including thos passed to the method in the method args not marked final. But it can access all instance vars of the enclosing class just like any other inner class. So it can access size in the above example if it wanted to. However, in this example it does not. You don't override vars rather you hide them. So innerclassinstance.size gives 37, but outerclass.this.size gives 42
[ July 20, 2003: Message edited by: Damien Howard ]
 
Sefa Urgenc
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Damien,
If its no trouble could you show that in code, the way the instance variable is accesed inside the method-local class ?
Thanx in Advance,
Sefa Urgenc
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Can the object a in the following statement ever access size (37) field of Dog Class? If yes could someone please tell me how?
Animal a = new Dog();
Thanks
Lalitha
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy -- there's no way you can use 'a' (without a cast) to access the Dog object's instance variable, since 'a' is of type Animal.
But you can do this (look at the last line of code):

So the answer is yes and no
No, you can't get to 37 (value of Dog's size) with 'a' directly, but yes you *can* get to it with a cast.
The longer version of:
System.out.println(((Dog)a).size);
would be:
Dog d = (Dog) a;
d.size; // 'd' is type Dog, so we get Dog's value
cheers,
Kathy
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kathy...
Lalitha
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To access the size vars with the animal reference follow Kathy's advice, but to just access the size vars in general you can do the following:
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Damien Howard:
To access the size vars with the animal reference follow Kathy's advice, but to just access the size vars in general you can do the following:


Hi Damien,
I believe your sample code will fail to compile.
For it to compile, the line:
System.out.println(Animal.this.size); // print's animal's size
should be replaced by:
System.out.println(((Animal)this).size); // print's animal's size
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am a bit confused with the size variable. Both Animal and Dog has a size variable? They are different although the names are same. If I use
System.out.println(a.super.size);
Will that work and print 42?
Thanks
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton
I think this statement
System.out.println(((Animal)this).size);
is not accessing the instance variable. Instead it is being casted to another class which has size object. This is just a coincidence that Dog Extends Animal.
If Dog didn't extend Animal class the above statement would not work.


[Code reformated by Val]
[ July 21, 2003: Message edited by: Valentin Crettaz ]
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I apologise for the previous post.
But the point I wanted to convey is if a Local Inner Class (a class declaration within a method code) hides an instance variable (example size) there is no way of accessing that instance variable. Please correct me if I am wrong.
Although this can be done in an Inner class (class declaration within a class) using the Damien suggested.
Lalitha
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lalitha Chandran:
Hi all
I apologise for the previous post.
But the point I wanted to convey is if a Local Inner Class (a class declaration within a method code) hides an instance variable (example size) there is no way of accessing that instance variable. Please correct me if I am wrong.
Although this can be done in an Inner class (class declaration within a class) using the Damien suggested.
Lalitha



Note: Is it just my browser or is there something wrong with this thread? The presentation in my browser(Mozilla) is all screwed up
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton
I think this statement
System.out.println(((Animal)this).size);
is not accessing the instance variable. Instead it is being casted to another superclass which has size object. This is just a coincidence that Dog Extends Animal.
If Dog didn't extend Animal class the above statement would not work. For example


The point I want to convey is if a Local Inner Class (a class declaration within a method code) hides an instance variable (example size) there is no way of accessing that instance variable. Please correct me if I am wrong.
Although this can be done in an Inner class (class declaration within a class) using the Damien suggested.

Thanks
Lalitha
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lalitha Chandran:
The point I want to convey is if a Local Inner Class (a class declaration within a method code) hides an instance variable (example size) there is no way of accessing that instance variable. Please correct me if I am wrong.
Although this can be done in an Inner class (class declaration within a class) using the Damien suggested.
Thanks
Lalitha


Hi Lalitha,
Ok, now I understand your question. I don't get quite what you mean by that instance variable.
The sample code provided cannot show you how to access a variable hidden by a local class. First of all, the instantation of the the 'Dog' class is done in the static context. At this point, you cannot access the size variable of the Animal class because there is NO instance of the Animal class yet (There is no Animal.this so that is why the program will not compile). Remember that size is an instance variable.
However, if there is an instance of Animal, then prefixing the 'this' with the class name, like what Damien did, will work:

Hope this helps
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i add an access method to the class dog/Animal

and when i do a.giveMeTheSize() will return 37 due to polymorphism.
So why is this inconsistency in the behavior
a.size //returns 42
a.giveMeTheSize() //returns 37
Can anyone explain??
[ July 22, 2003: Message edited by: Sudhakar Krishnamurthy ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sudhakar Krishnamurthy:
If i add an access method to the class dog/Animal

and when i do a.giveMeTheSize() will return 37 due to polymorphism.
So why is this inconsistency in the behavior
a.size //returns 42
a.giveMeTheSize() //returns 37
Can anyone explain??
[ July 22, 2003: Message edited by: Sudhakar Krishnamurthy ]


Everytime you call an instance method, it receives an 'extra/hidden' parameter - the this reference. The method will use this to qualify those unqualified variables. So in your example method, the call return size is equivalent to return this.size.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic