aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Reg. Casting Object References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt Reg. Casting Object References" Watch "Doubt Reg. Casting Object References" New topic
Author

Doubt Reg. Casting Object References

Ravi Pinnaboyina
Greenhorn

Joined: Feb 19, 2007
Posts: 11
hi frenz,

I had a doubt regarding Casting Objects to one another I understood about implicit casting, But when it comes to explicit conversions when does it throw Compile time Error and when does it give Runtime Exception(Classcast Exception).
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
As you know the first thing that could happen is a compiler error and then the runtime error.

(Remember this 2 points

1.- If the object you are trying to cast is not from the same hierarchy of the class to promote it leads to compiler error.

2.- Once it compiles, If the object you are trying to cast is in the hierarchy but does not apply for an "is a" relationship it leads to java.lang.ClassCastException.


To clarify this:

Imagine you have 3 classes.

Animal (An abstract animal, maybe it has 8 legs or 4 who knows)

Dog (An specific animal, it has 4 legs for sure)

Cat (An specific animal, it has 4 legs too)

And the declarations of classes are:

class Dog extends Animal{}
class Cat extends Animal{}


Lets point out what you can do and what you can't do (---- means independent):

You can:
--------------------
Dog d = new Dog();
--------------------
Cat c = new Cat();
--------------------
Animal a = new Dog();
--------------------

Easy, isn't it?

You can't:
---------------------
Dog d = new Dog();
Cat c = new Cat();

c = (Cat)d; // Compiler error!!!. Explanation: (see number 1)
----------------------

Animal a = new Animal();
Cat c = new Cat();

c = (Cat)a; // ClassCastException!!!. Explanation (see number 2)


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Ravi Pinnaboyina
Greenhorn

Joined: Feb 19, 2007
Posts: 11
Is this same even in case of Interfaces.... Does we get a class cast Exception when using Interfaces?

Does the same rules apply to casting objects to interfaces?
[ February 19, 2007: Message edited by: Ravi Pinnaboyina ]
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
Yes its the same.

Almost all in Java is the same between classes and interfaces (in that sense (is a relationship)), except for the "instanceof" operator in which you can use any interface after "instanceof" and you won't get compiler time error. But if you use a class that is not part of the hierarchy you get compiler error.
Ravi Pinnaboyina
Greenhorn

Joined: Feb 19, 2007
Posts: 11
thanks Javier Now Iam clear with this.
Priya Viswam
Ranch Hand

Joined: Dec 28, 2006
Posts: 81
Please go through this

http://www.coderanch.com/t/261446/java-programmer-SCJP/certification/Help-Assigning-one-Reference-another


SCJP 1.5<br />SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt Reg. Casting Object References