Forums Register Login

Polymorphism

+Pie Number of slices to send: Send
In K&B's book, under the polymorphism topic, it says :
'at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. Not variables'

I think and prove that instance variables are also dynamically selected.

Am I right?
+Pie Number of slices to send: Send
 

Originally posted by j . thexyz:
In K&B's book, under the polymorphism topic, it says :
'at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. Not variables'

I think and prove that instance variables are also dynamically selected.

Am I right?



Hi J.

Are you asking about anything then it would be great if you would have been give some example.

So if you are going to prove something then , Please let all of us know what you want to prove and how???
+Pie Number of slices to send: Send
Why dont you give us your example and we ll tell you. Also... please change your name to fit java ranch naming policy
+Pie Number of slices to send: Send
"j thexyz",

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please pay attention and select a valid diaplay name or your account will be closed.

thanks,
Dave
+Pie Number of slices to send: Send
All I mean is, if the subclass has declared an instance variable with the same name as that in superclass, the subclass variable is dynamically invoked.

For eg:
class GameShape {
int i = 10;
// more code
}

class PlayerPiece extends GameShape {
int i = 20;
// more code
}

}

public class Test1{
public static void main (String[] args) {
PlayerPiece shape = new PlayerPiece();
System.out.println("i= "+shape.i);
// more code
}
}

OUPUT IS 'i= 20'

Am I thinking in the right direction?
+Pie Number of slices to send: Send
Hi,

Your example has a mistake. The compiler selects i=20 based on shape type, PlayerPiece.

It's on compile time, not dynamically.

In my example you can see it more clearly


class A {
public int v = 1;
}

public class B extends A {
public int v = 2;

public static void main(String args[]){
A obj1 = new A();
A obj2 = new B();

System.out.println(obj1.v + " - " + obj2.v);
}
}

The output is:
1 - 1
+Pie Number of slices to send: Send
Thanks v much.
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 593 times.
Similar Threads
OverRiding
Reg:Override and redefine
Problem in polymorphism
Doubt in K&B SCJP 5: topic - Overriding
Doubt Regarding Overriding Method,Variables
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:50:20.