aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
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?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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???


The Best way to predict your future is to create it - Every great individual common man
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

Why dont you give us your example and we ll tell you. Also... please change your name to fit java ranch naming policy


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"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
jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
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?
Leonardo Luiz
Ranch Hand

Joined: Mar 24, 2006
Posts: 51
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
jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
Thanks v much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism
 
Similar Threads
INSTANCE VARIABLES
overriding and polymorphism
public Class variables and inheritance
Question from Exam lab practice Exam 1 about polymorphism
Doubt Regarding static reference