Results 1 to 8 of 8
  1. #1
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Arrow Casting a child class into a parent class.

    Hi there, new to the forums and plan on staying for a very very long time. :o

    My question, as the title states:

    -Is it possible to cast an object of a child class into an object of its parent class and if so, how is it coded?

    This is what I've been tinkering with thus far..

    Example:

    ChildClass a = new ChildClass();
    ParentClass b = (ChildClass)a;



    -Unsub
    Last edited by Unsub; 01-28-2010 at 08:37 AM. Reason: edit 1: added example

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    The ChildClass object is already a ParentClass object so there is no need to cast.
    Putting the cast is just redundant.

  3. #3
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    The ChildClass object is already a ParentClass object so there is no need to cast.
    Putting the cast is just redundant.
    Got it.

    What about the other way around, could I cast a parent class into a child class?

    -please forgive me if these seem like noobish questions. I've just entered the world of programming and am just starting to dive in to it all.
    Last edited by Unsub; 01-28-2010 at 08:59 AM. Reason: edit: #1:Fixed quote

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You can. The compiler won't complain. You could get a runtime exception though if the object cannot be cast to that type at runtime.

  5. #5
    rummy is offline Member
    Join Date
    Jan 2010
    Posts
    21
    Rep Power
    0

    Default Casting related to inheritance

    Here by i give the casting procedures and problems,related to inheritance


    superclass=subclass (This is valid)


    subclass=(subclass)superclass (This is valid at compile time but shows an error in run time-ClassCastException)


    subclass=superclass (Shows error in compile time itself)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    subclass=(subclass)superclass (This is valid at compile time but shows an error in run time-ClassCastException)
    No it doesn't...see:
    Java Code:
    Superclass superc = new Subclass();
    Subclass subc = (Subclass) superc;
    is perfectly valid at runtime.

  7. #7
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default

    I think I understand now. Thanks everyone.

  8. #8
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    I think it's called polymorphism.

    If you extend it, you can use the superclass type & connect it to the subclass.

Similar Threads

  1. how to get the child class in inheritance?
    By java_fun2007 in forum New To Java
    Replies: 7
    Last Post: 09-29-2010, 09:35 AM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  4. Parent & Child window issues......
    By jithan in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 09:21 AM
  5. class casting
    By vital parsley in forum New To Java
    Replies: 1
    Last Post: 07-24-2008, 10:03 AM

Posting Permissions

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