Results 1 to 6 of 6
Thread: Object Casting, please help
- 12-04-2010, 09:13 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
Object Casting, please help
I am learning Java for the first time, and the only problem I am having right now is understanding object casting. Here is a quick example that does not work and I do not understand why:
class A {
}
class B extends A {
}
class ObjectCaster {
public static void main(String[] arguments) {
A a = new A();
B b = (b)a;
}
}
This always results in an error for me. The reason I am really perplexed is that the book I am studying (Sams Teach Yourself Java in 21 Days) shows a code snippet that apparently works, and is done in the same fashion:
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D)comp;
... // more code
}
I don't understand why Graphics2D comp2D = (Graphics2D)comp; would work, because I am doing the same thing in my first example of code. A is the superclass, like Graphics, and B is the subclass, like Graphics2D. I define an object of type A and pass it with a cast to the object of type B and it doesn't work. However doing so with a Graphics object to a Graphics2D object, which is the same thing to my understanding, apparently works. I just don't get why...they should both be doing the same thing essentially. Please help me understand this, I don't want to be confused about object casting when I finally get to a point in my Java programming that I need to take advantage of it. Thanks a lot everyone,
-Derek
- 12-04-2010, 10:32 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Inheritance is a is a relationship; your B object is anA object. You can treat any B as if it were an A. The other way around isn't true; you could have done in your code:
Casting objects from one class to another doesn't change the object at all; it just changes how we can view that certain object, but it still has to be such an object. In your example you created an A object which isn't a B object but you were trying to cast that A object as if it were a B object.Java Code:A a= new B(); // a B is an A B b= (B)a; // so this works
A Graphics2D object is a Graphics object but it was passed to you as if it were a Graphics object; you can always cast it back to a Graphics2D object.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-04-2010, 11:22 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
I now understand what you are saying in regards to casting the B object to A, as long as A is instantiated as an object of type B.
What confuses me is the way the Graphics object is able to be casted to a Graphics2D object. Am I missing something about the comp object? In the procedure I showed in the example it is passed as a Graphics object and then cast to a Graphics2D object. Was the Graphics object instantiated as a Graphics2D object and I just didn't see that part of the code?
I am confused because Graphics is a superclass of Graphics2D, just as A is a superclass of B. If Graphics2D comp2D = (Graphics2D)comp, and comp is a Graphics object, then shouldn't it be allowed that B b = (B)a? I thought they were essentially the same because of their superclass to subclass relation. Sorry if I am trying to dig to deep on this matter, but if there is something I am missing about the Graphics and Graphics2D classes that make them a different case from my A and B classes it would really help to know. Thank you again for your time and patience and for responding so quickly!
-Derek Raimann
- 12-04-2010, 11:41 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You're on the right track: before your paintComponent( ... ) method is called Swing creates a Graphics2D object for you but passes it to your method as if it were a Graphics object (Graphics2D extends Graphics). This is all for backward compatibility reasons: AWT used to create Graphics objects for you and they (Sun) wanted the new code to be compatible with the old code. Since that object really is a Graphics2D object you can cast your Graphics object to a Graphics2D object. In general: you can always 'upcast' your object, i.e. cast to a superclass of the class you're dealing with, but you can only 'downcast' an object if and only if your object actually is a member of that other class you want to cast to. So if you create a B and pass it around as if it were an A, everyone is allows to cast it back to a B again (because it really is a B). If you had created an A noone can cast it to a B because it isn't a B.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-04-2010, 12:50 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
Wow, you cleared up that whole confusion after explaining that the Graphics comp object passed was actually a Graphics2D object. Now that the confusion has settled I think I can confidently move on with my studies. Thanks again for all of your timely help, it is all greatly appreciated Jos! :)
-Derek Raimann
- 12-04-2010, 01:14 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You're welcome of course; the rule (basically) is very simple: suppose you have the following list if classes A --> B --> C --> D ... where B extends A, C extends B, D extends C etc. You can always 'upcast' from a class to one on the left and you don't even need to explicitly cast it, e.g. A a= new B();
Casting 'downwards' (to the right in this example) alsways has to be done explicitly and only if the object actually is of the target class (or a subclass thereof).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Casting Object to another type
By green_river48 in forum New To JavaReplies: 12Last Post: 04-03-2010, 10:52 AM -
Object casting
By spiderweb in forum Advanced JavaReplies: 5Last Post: 08-20-2009, 05:43 PM -
Casting
By zzpprk in forum Advanced JavaReplies: 13Last Post: 08-13-2009, 07:59 PM -
casting help
By soc86 in forum New To JavaReplies: 4Last Post: 01-13-2009, 11:07 PM -
Object to Int casting
By nn12 in forum New To JavaReplies: 4Last Post: 12-06-2008, 10:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks