Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object referernce casting

Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Base {}
class Sub extends Base {}
public class CEx {
public static void main (String[] args) {
Base b = new Base();
Sub s = new Sub();
// b = s; // works
// s = b; // incompatible types-Error when compiling
s = (Sub)b; // Class Cast Exception Runtime Error
I have problems to understand object reference casting.
In the coding stated above I create a superclass object (Base b) and a subclass object (Sub s). It is possible that a superclass object b can refer to a subclass object s. The code �b = s� works (no comiliation or runtime error).
But I didn�t manage to refer subclass object s to superclass object.
s = b => Compiler error: Incompatible Types
s = (Sub) b => Runtime error: Class Cast Exception
There is a rule for object reference casting that states that the class of the expression being converted (=> b) must be the �NewType� or must inherit from �NewType�. Here �NewType� is Sub.
Can anyone give further explanation why a subclass object can�t refer to a superclass object whereas vice versa it is possible?
Perhaps you can provide me with some links regarding object reference casting.
Thank you very much.
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine you have two class, Fruit and Apple. Apple is a child of Fruit. Therefore you can say, "Apple is a Fruit" but you can't say that "Fruit is an Apple".
Fruit f = new Fruit();
Apple a = new Apple();
f = a; // works fine because Apple is a Fruit
a = f; // compile error because Fruit is not always an Apple
Now it may be that Fruit is an Apple but the compiler doesn't know that this is the case so you must cast the Fruit to an Apple.
a = (Apple)f; // compiles OK
The problem is that if you lied to the compiler and f really isn't an Apple then you will get a runtime error.
Fruit f = new Apple();
Apple a = (Apple)f; // no problem at compile or runtime!
Roberto Ingunza
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to understand that there are objects and there are pointers. For example:
String s; // a pointer.
new String("Pepe");// an object
String s = new String("Pepe");//an object assigned to a pointer.
That means that you can have any number of pointers pointing to an object. Now, the pointer defines exxactly HOW you can access that object, but it doesn't CHANGE the object.
class A{ defines methods met1() and met2()}
class B extends A{defines method met3()}
When you instanciate a class, the mechanism involves the invocation of the constructor of the superclass. In our example:"new B();" will involve the invocation of the constructor of A and B, but "new A();" will involve only the invocation of the A constructor. Indirectly, every object have a reference to an object of each and everyone of the parent classes (its superclass and the superclass of its parent, and so on)
You can have this assignment:
A obj1 = new A();
and this:
B obj2 = (B)obj1;
But remember that when you invoke the A constructor, you create the A object and nothing else (well, actually you create an Object object too) thus an exception will arise. But if you do this:
A obj1 = (A)new B();
invoking the B constructor will involve the creation of an A object, so the assignment is correct and no exception will be thrown.
Remember: instantiation of a class ALWAYS causes the instantiation of the superclass. That is the explanation.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic