Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you give us your example and we ll tell you. Also... please change your name to fit java ranch naming policy
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks v much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic