aspose file tools*
The moose likes Beginning Java and the fly likes Object referernce casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object referernce casting" Watch "Object referernce casting" New topic
Author

Object referernce casting

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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
}
}
Hello,
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.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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!


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Roberto Ingunza
Greenhorn

Joined: Mar 12, 2002
Posts: 4
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.
Example:
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object referernce casting
 
Similar Threads
Narrowing reference
type casting
Run time casting of Objects
cast
ClassCastException