• 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 casting

 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL!I am a little bit confused over object reference casting.Help me out guys ,my deadline is approaching!
class A {
void method(){
System.out.println(" in A");
}
}
class B extends A {
void method(){
System.out.println(" in B");
}
}
class C extends B {
void method(){
System.out.println(" in C");
}
}
public class MyClass{
public static void main(String args[]){
A a=new C();
B b=(B)a;
b.method();//prints "in C" no problem here
C c=new C();
b=c;// shouldn't this give me a compiler or runtime error??
if(b instanceof C)
System.out.println("true");// this prints true why?
}

}
Do let me know.
Thanks all,
Vedhas.


 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, even i got confused.. i just tried more instanceof conditions and it all seem to be true.. !


class A {
void method(){
System.out.println(" in A");
}
}
class B extends A {
void method(){
System.out.println(" in B");
}
}
class C extends B {
void method(){
System.out.println(" in C");
}
}
public class MyClass{
public static void main(String args[]){
A a=new C();
if( a instanceof C) // this is true
System.out.println("Yes, a is instanceOf C");
if( a instanceof A) // THIS IS TRUE ?
System.out.println("Yes, a is instanceOf A");
B b=(B) a; // HERE,WHICH OBJECT IS ACTUALLING GETTING CASTED...I THINK C..?
if(b instanceof C) // THIS IS ALSO TRUE
System.out.println("Yes, b is instanceOf C");
b.method();//prints "in C" no problem here

C c=new C();
b=c;
// shouldn't this give me a compiler or runtime error??.NO AS B IS OF TYPE C..SO C IS ASSIGNED TO C

if(b instanceof C)
System.out.println("true");// this prints true why?..THAT IS WHY IT PRINTS TRUE...
}
}


Comments
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doubt 1: b=c;// shouldn't this give me a compiler or runtime error??
explanation 1: This is a case of automatic conversion. Since C extends B, c is "up the inheritence tree" kind of conversion which is legal.
Doubt 2: if(b instanceof C)
System.out.println("true");// this prints true why?
}
Explanation 2: It returns true b/c b refers to a C object(c)..... thereby b (which is of type C)instanceof C will return true.
Hope it helps,
V
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic