• 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: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks v much.
 
reply
    Bookmark Topic Watch Topic
  • New Topic