aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Doubt about ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Doubt about ClassCastException" Watch "Doubt about ClassCastException" New topic
Author

Doubt about ClassCastException

Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
I would like to get some help for the below question I found in Enthuware exam:



The answer is Line 2.
But I have a query here. Once a is assigned to b then its points to the b object. And in line 3 b=a,therfore b must simply point to the B object only. How can a ClassCastException be generated here?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

As far as I know ClassCastException is a RuntimeException, the exam question is about compilation failure. Also you are mentioning "line 3 b=a", but that's not line 3, it's line 2. So not sure if this is related to the original exam question or if it's a doubt of your own


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
As I mentioned before that the question is from enthuware.
And i am confused about the answer.
please explain.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Why do you think a ClassCastException will be generated at line 2? Like I said before ClassCastException is a runtime exception, not a compilation failure.
Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
I don't feel that a ClassCastException must be generated.
Because at Line 1 if a=b, then when line 2 runs, b=a literally means that b s pointing again to its own B object because a is already pointing to the same in Line 2.
I feel that the code must run fine. But the answer is not that.
I want to know the flow of the program.
Explanation would be appreciated.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Have a look at this Java Tutorial. It's about inheritance and you definitely have to take a close look at the section "Casting Objects". I'm quite certain it will clear your doubts and you'll be able to answer your question yourself.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

It might be easier to understand if you use more intuitive classes, rather than A and B.

Consider this:
Now, should that final line be valid? At that point animal could be referencing a Cat, a Dog, a Mouse, a plain Animal etc. Can you guarantee that it is safe to assign it to a Cat reference? No. So the compiler won't allow it.

If you used a cast:
Now you're overriding the compiler, telling it "trust me, it's a Cat". The compiler will allow it, but then you'll get a ClassCastException at runtime if it's not actually true.
Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
A subclass reference cannot refer to a superclass object. That would give a compiler error. But in line 1 a=b.... That means A class reference points to the B class.
So when b=a, at that time a is already pointing to the B object. So indirectly b points to its own object.
Maybe if line 1 was removed, then I could have understood that there would be a compiler error on line 2. If I m wrong then how and how does line 1 and 2 work?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

For assignments it's the type of the reference variable that matters, not the type of object it's refering to.

So even this one would not compile:


You have to promise the compiler that you know what you are doing and that you'll assign a B object with an explicit cast. This code will compile:


And what do you think will happen with this code snippet? Compiler error? Runtime exception? Running without any exception?
Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
The last code will run fine I guess...
Well I did understand what you explained. finally its the reference variable that decides the assignment type.

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Seema Shrikant wrote:The last code will run fine I guess...

It will run, so no compilation failure. But it won't run fine. Give it a try yourself and see what happens.
Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
ClassCastException!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Indeed! And do you know why?
Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
Yes.
Because A refers ro its own A class object, so even after a cast, the subclass B will actually point to a A class object, which is its superclass.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Indeed! a is an instance of A and will never be B (despite the explicit casting)

Bonus question: do you know how you can prevent such a ClassCastException when you have to explicit cast an object?
Seema Shrikant
Greenhorn

Joined: May 17, 2013
Posts: 18
We can use the Exception handling of course to handle RuntimeExceptions also.
But better way we could just make a refer to a B class object if class B extends class A.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

You normally don't catch/handle runtime exceptions. You have to prevent a runtime exception from happening (if possible) and in most cases it is possible (e.g. adding a null check, checking the length of an array or a list,...)

Let's take a look at a method that converts all of the characters of a given string to lower case:


But that could result in a NullPointerException when calling toLowerCase(null);, so instead of catching and handling this runtime exception we prevent this exception from happening like this:
Problem solved!



Now it's up to you for the bonus question! Consider the code snippet below. Implement the getB() method (and that's the only thing you can do) in such a way that this little program has no compilation errors and when you run the program also no runtime exceptions are thrown.

Good luck!
 
 
subject: Doubt about ClassCastException