• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic