• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

reference confusion

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

danchisholm mocks ....


class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B(); A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}

What is the result of attempting to compile and run the program?
a. Prints: B.s1 A.s2 B.s1 A.s2
b. Prints: B.s1 A.s2 A.s1 A.s2
c. Prints: A.s1 A.s2 B.s1 A.s2
d. Prints: A.s1 A.s2 A.s1 A.s2
e. Run-time error
f. Compile-time error
g. None of the above

answer : Prints: B.s1 A.s2 A.s1 A.s2

explanation ?
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its right. Remember that an object's instance variables are chosen based on their reference variables. Polymorphism doesn't apply to instance variables, only methods.


 
rohit surve
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya i know
but still s2 is a varibale in class A...and not in class B THEN x.s2 gives you A.s2 BUT reference of x is of type B...HOW? thats the doubt
 
rohit surve
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya i know
but still s2 is a varibale in class A...and not in class B THEN x.s2 gives you A.s2 BUT reference of x is of type B..pleas explain as i am getting confused in this silly doubt...
 
Ryan Beckett
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but still s2 is a varibale in class A...and not in class B THEN x.s2 gives you A.s2




Huh?!?! s2 is a variable in class B. Ha. Its inherited man.


B x = new B();

B is the reference type here. So, we REFER to the B class type definition.

A y = //whatever you want

A is the reference type here. So, we REFER to the A class type definition.


I can't explain it anymore than that. It's one simple thing to remember, 'we choose instance variables based on reference'. It would be different if we were talking about polymorphism, but were not.



If you run this then you'll that the two calls to foo(), print "B", now the reference types don't matter (its polymorphism), its based on the object type.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ryan,

you're the man!

finally i've got it too. These things were the most misconceived topics for me.


The last example of code made it all clear.


I've got to burn this in:

When there are methods on the game i've got to look at the object type.

When there are instance variables i must look at the reference type.



But just to clear this out, once and for all:
what happens in line 11?

object reference x references an Object of type B (which extends A).

with the assignment above i'm doing just a copy of reference x to reference y - right? That means im not changing the objects type, right? Im just saying: y is a reference of type A and and on the heap, there is an object of type B, referenced by x.

So when i'm doing polymorphism like here



the foo method of class B is invoked, because the Object is of type B.


I am asking because i'm wondering about this
I always pretended that (through (A)x ) the object referenced by y is from now on of type A, but it's just a upcast - is'nt it?
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags, it makes things so much easier to follow, thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic