Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Inheritance Casting question

Options
  • 25-04-2014 4:57pm
    #1
    Registered Users Posts: 1,154 ✭✭✭


    I'm just trying to get my head around inheritance and casting. Could anyone confirm that my assertions below, based on the classes here is correct?
    class A {}
    class B extends A {}
    

    Obviously, the two following declarations are OK:
    A a = new A();
    B b = new B();
    

    This is allowed, as A (Superclass/more general) is the reference type, and B(subclass/more specific) is the object type.
    A a = new B();
    
    As "a" is originally an object of type B it can be cast back to B without any problems
    B b = (B) a;
    

    Trying to have the Subclass as the reference type and the Superclass as the object type will result in a compiler error, unless we explicitly overrule the compiler using an explicit cast of type B:
    B b = (B) new A();
    

    However, while this will bypass the compiler check, a ClassCastException will be generated at runtime because although we told the compiler that "b" is of type B, in fact it is of type A instead.

    So, is there ever a case where the reference type is a Subclass and the object type is a Superclass which will compile and run without issue?

    Also, are there any other valid or invalid scenarios not listed which I'm missing?
    Thanks in advance


Comments

  • Registered Users Posts: 403 ✭✭counterpointaud


    johnnykilo wrote: »
    Trying to have the Subclass as the reference type and the Superclass as the object type will result in a compiler error, unless we explicitly overrule the compiler using an explicit cast of type B:
    B b = (B) new A();
    

    However, while this will bypass the compiler check, a ClassCastException will be generated at runtime because although we told the compiler that "b" is of type B, in fact it is of type A instead.

    So, is there ever a case where the reference type is a Subclass and the object type is a Superclass which will compile and run without issue?

    I don't think so and (unless I am missing something) I can't think of any reason why you would want to try and do this?

    I have seen lots of situations where Class B extends Class A and Class A implements interface I:
    I b = (I) new B();
    

    But this is a different scenario to the one you put forward.


  • Registered Users Posts: 1,154 ✭✭✭johnnykilo


    Yeah you're probably right. I'm just studying for the OCJA at the moment and trying to get my head around all the possible casting options. Like you said there probably is no practical application of the example you highlighted but it doesn't stop them asking questions about it :P

    But yeah the Interface casting is something which I hadn't thought about. I'm sure that could also possibly come up. Thanks!


  • Registered Users Posts: 403 ✭✭counterpointaud


    johnnykilo wrote: »
    Yeah you're probably right. I'm just studying for the OCJA at the moment and trying to get my head around all the possible casting options. Like you said there probably is no practical application of the example you highlighted but it doesn't stop them asking questions about it :P

    But yeah the Interface casting is something which I hadn't thought about. I'm sure that could also possibly come up. Thanks!

    Ah right, I heard there can be awkward questions on that exam alright. Best of luck with it. Can't check now as I am on a phone, but won't all invalid casts get past the compiler, and only throw exception at runtime?

    EDIT: On re-reading, I think you were not disputing this in your post, just that a cast was needed to keep compiler happy.


Advertisement