Results 1 to 6 of 6
  1. #1
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    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:

    Java Code:
    A a= new B(); // a B is an A
    B b= (B)a; // so this works
    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.

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DerekRaimann View Post
    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
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DerekRaimann View Post
    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! :)
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Casting Object to another type
    By green_river48 in forum New To Java
    Replies: 12
    Last Post: 04-03-2010, 11:52 AM
  2. Object casting
    By spiderweb in forum Advanced Java
    Replies: 5
    Last Post: 08-20-2009, 06:43 PM
  3. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 08:59 PM
  4. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  5. Object to Int casting
    By nn12 in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 11:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •