• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Superclass to Subclass Casting

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on some sample code involving casting and could use a little help with this.
Here's my example:
class A implements T1, T2{}
class B extends A implements T1{}
interface T1 { }
interface T2 { }
A a = new A(); //1.
B b = new B(); //2.
b = (B)a; //3.
Can someone please explain to me why the ClassCastException occurs at Runtime at 3? Shouldn't this cast be permitted since the cast is used from a superclass type to a subclass type?
Any help is appreciated.
CG
 
Author & Gold Digger
Posts: 7616
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting can be tricky business sometimes.
Let's change a little bit the name of the classes you have there:
class Animal implements T1, T2{}
class Dog extends Animal implements T1{}
interface T1 { }
interface T2 { }
Animal a = new Animal(); //1.
Dog b = new Dog(); //2.
b = (Dog)a; //3.
OK on line 1, we create a new reference "a" of class Animal pointing to a new object of class Animal.
On line 2, we create a new reference "b" of class Dog pointing to a new object of class Dog.
Now, what is line 3 trying to do? We are trying
to convert an Animal to a Dog. How would that be possible? What if the Animal reference "a" was pointing to some other subclass of Animal, like Duck or Cat or whatever, how could we possibly cast a Duck (or Cat) to a Dog?
Now look at the following, slightly different code:
Animal a = new Dog(); //1.
Dog b = new Dog(); //2.
b = (Dog)a; //3.
See on line 1 we create a new reference "a" of class Animal but to an new object of class Dog.
On line 3, "a" really points to an object of type Dog and thus the casting is allowed and nobody complains.
One more. Imagine we have a class Cat like this
class Cat extends Animal implements T1{}
and following objects:
Animal a = new Cat(); //1.
Dog b = new Dog(); //2.
b = (Dog)a; //3.
Then line 3 is wrong again since you cannot cast a Cat obejct to a Dog object.
Remember that the runtime type of the object is important while casting not the compile-time type.
Remember that you cannot cast an object of the superclass type to the subclass type unless the superclass type reference point to an object of its subclass type.
HIH
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian
Take a look at this, it is the heirarchy you described in your example:

First off, since B extends A and A already implements T1 there is no need for B to also implement it - it wil get all of the methods and variabels through its extension of A.
Now your question:
When you create the variable a you are creating an instance of an A object. If you look at the hierarchy when you create an A you are not also creating a B so when youn try to cast it there is no actual B object to cast it to.
On the other hand if you did this:
B b = new B();
A a = (A)b;
That is ok because when you created b you created a B object and a B object is a A object through inheritance. While an A object is not neccesarily a B object.
hope that helps
 
Christian Garcia
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin and Dave,
Thank you for the explanations. I realized that what I was doing wrong had to do with the type of references I was dealing with. In my previous example:

Since the code at (1) assigns the subclass reference to a reference of the superclass type the cast at (2) will now succeed.
This stuff is a little tricky. I guess it's just a matter or recognizing the class relationships and the reference assocations.
Thanks!
CG
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!