wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference variable downcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reference variable downcasting" Watch "reference variable downcasting" New topic
Author

reference variable downcasting

Luckson Karikoga
Greenhorn

Joined: Apr 27, 2010
Posts: 13
Hello world,
This is my first post so please bear with me.

I'm studying for SCJP 6 using Kathy Sierra and Bert Bates' study guide.

My question is what is the impact of the instanceof operator in snippet of code below?

class Animal {}
class Dog extends Animal {}
public class DogTest {
public static void main(String[] args) {
Animal animal = new Animal();
if (animal instanceof Dog) {
Dog d = (Dog) animal;
}
}
}

This code compiles and run fine, however, if you omit the instanceof test it gives a ClassCastException.

Thanks in advance.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Luckson Karikoga wrote:Hello world,
This is my first post so please bear with me.

I'm studying for SCJP 6 using Kathy Sierra and Bert Bates' study guide.

My question is what is the impact of the instanceof operator in snippet of code below?

class Animal {}
class Dog extends Animal {}
public class DogTest {
public static void main(String[] args) {
Animal animal = new Animal();
if (animal instanceof Dog) {
Dog d = (Dog) animal;
}
}
}

This code compiles and run fine, however, if you omit the instanceof test it gives a ClassCastException.

Thanks in advance.


You cant directly downcast, Dog is an Animal but Animal is not a Dog, use of instanceof tells JVM that yes i know that is wrong but allow me to do so,and please use code tag and mention source of post always.


Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Luckson Karikoga wrote:Hello world,
This is my first post so please bear with me.

I'm studying for SCJP 6 using Kathy Sierra and Bert Bates' study guide.

My question is what is the impact of the instanceof operator in snippet of code below?

class Animal {}
class Dog extends Animal {}
public class DogTest {
public static void main(String[] args) {
Animal animal = new Animal();
if (animal instanceof Dog) {
Dog d = (Dog) animal;
}
}
}

This code compiles and run fine, however, if you omit the instanceof test it gives a ClassCastException.

Thanks in advance.


consider the following snippet of code that you have written above
Animal animal = new Animal();
if(animal instanceof Animal)
Dog d =(Dog)animal;

here as said by Saloni above, you are downcasting animal reference to Dog. Now cast is simply an instruction or say a request to the JVM that i know what i'm doing. that i know the animal reference is really of type Dog. it is my responsibility. but beware the cast can fail at runtime . had you not written the instanceof check it would have . so here instanceof is like a safety mechanism . in the if condition you are making sure that animal reference really points to Dog object. if it is then only to the cast , otherwise not.
Luckson Karikoga
Greenhorn

Joined: Apr 27, 2010
Posts: 13
Thank you for your replies.

My code in from page 117, with a little modification.

I understand that you acknowledge the risks with downcasting. However if you compile and run the original code from the book without the instanceof test, you run into a ClassCastException. If you modify the original code from book with the instanceof test the application will compile and runs fine.

My question is what is the other magic of instanceof besides testing for the IS-A relationship?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Luckson Karikoga wrote:Thank you for your replies.

My code in from page 117, with a little modification.

I understand that you acknowledge the risks with downcasting. However if you compile and run the original code from the book without the instanceof test, you run into a ClassCastException. If you modify the original code from book with the instanceof test the application will compile and runs fine.

My question is what is the other magic of instanceof besides testing for the IS-A relationship?


instanceof operator will return true if IS-A test succeeds and false otherwise. there is no other magic. instanceof operator as the name suggest is used for IS-A relationship check. in your example it will return false since animal reference points to animal object and not Dog object. and as saloni pointed out Every Dog is an animal but not vice versa. so instanceof operator is there to avoid the exceptional condition when we do invalid downcast
Luckson Karikoga
Greenhorn

Joined: Apr 27, 2010
Posts: 13
Thanks Gurpeet ,and Saloni. Its now clear. I appreciate your time spent.
Luckson Karikoga
Greenhorn

Joined: Apr 27, 2010
Posts: 13
The downcasting won't run because the if test fails.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Luckson Karikoga wrote:The downcasting won't run because the if test fails.


Yes, now you're learning slowly.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Luckson Karikoga wrote:The downcasting won't run because the if test fails.

Right
Luckson Karikoga
Greenhorn

Joined: Apr 27, 2010
Posts: 13
Yah the ranch really helps.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Luckson Karikoga wrote:Yah the ranch really helps.

 
 
subject: reference variable downcasting
 
Similar Threads
java.lang.InstantiationException
Reg. casting from Khalid Mughal
instanceOf doubt...
Confused with instanceof, please help
class cating