Results 1 to 2 of 2
  1. #1
    letdoit is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Class Inheritance Cast

    Hi,
    If I have:
    1. Account a = api.getAccount();
    2. AccountX extends Account;

    Then I do:
    AccountX x = (AccountX) api.getAccount();

    I got runtime class cast exception. Could you explain why?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You get a runtime ClassCastException whenever you try and cast something whose runtime type is not what you are casting to (or a subtype of what you are casting to). In your case api.getAccount() returned something that was not an instance of AccountX.

Similar Threads

  1. Need Help with Class Cast Exception
    By soxfan714 in forum New To Java
    Replies: 2
    Last Post: 11-03-2011, 04:33 PM
  2. Class cast issue
    By AedonetLIRA in forum New To Java
    Replies: 5
    Last Post: 11-19-2010, 04:34 PM
  3. Cast object to custom class problem
    By trader5050 in forum New To Java
    Replies: 7
    Last Post: 11-15-2010, 10:47 AM
  4. Cast own class to FrameView
    By Luxpsycho in forum AWT / Swing
    Replies: 0
    Last Post: 07-16-2010, 10:13 AM
  5. class cast exception
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 09:50 PM

Posting Permissions

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