• 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

object reference

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey 'all
This is the code..
class A{
int i =1;
int f(){return i;}
class B extends A{
int i=2;
int f(){return -i;}
public class Test{
public static void main(String arg[]){
B b=new B();
A a=(B)b;
System.out.println(a.i);//prints i of A. ie, 1
System.out.println(a.f());// prints -2
}
}
Though Iam assgining a reference of B to A a ,a is acting like declared type(i.e A type)..
Iam curious that is there any way we can change the reference type ..
thnx
rishi
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(a.i);//prints i of A. ie, 1
a in this statement is of type A. And for fields the reference types matter not object types. Therefore, field from class A is printed.
System.out.println(a.f());// prints -2
a in this statement holds an object of type B. And for methods object type matters not reference type. Therefore, method from B is called.
Thanks
Barkat
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barkat I got that right...but what I wanted to know is there anyway we can change the reference type..
I mean for example in the above code reference type of a is always Type A...
is there any possibility I can change it to Type B so that a.i prints 2..
rishi
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rishi
i dont see anyway of "changing" the declared Reference type. but u can try,
System.out.println(((B)a).i);
that would give u result 2...
once u declare something it will have that type as the reference type. now, u can hold any compatible object in that reference like in this example u've type B hold in ref a even it it was declared of type A BUT u can't "re-declare" ref a to be of B type.
regards
maulin
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rishi:
It is possible to change the type (as per other post).

It only possible because actual object refered to by A is B. Another example is:
Say actual object type is Man. { class B in your example }
We refer to it as man { reference b in your example }
We can also treat it as human { reference a in your example }
But we can also say treat that human as man now { ((B)a) in your example }
If actual object type was Human, we could not say the last line above.
I hope it makes sense.
Barkat
 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic