cresjoy cresjoy - 1 year ago 97
Java Question

Casting between Interfaces and Classes

Yesterday I just started learning interfaces and have been doing some simple examples, I noticed that I have had lots of trouble understanding casting between classes and interfaces so I read Java Cast Interface to Class as well as Interfaces Oracle Java Tutorials

However when my book gave a small review question at the bottom, I noticed that I still didn't really understand completely, and the book I bought doesent have solutions. The following is the question ( I gave my attempt and reasoning which is probably wrong for some, so any help is good!)

Suppose the class Sandwich implements the Edible interface, and you
are given the variable declarations

Sandwich sub = new Sandwich();

Rectangle cerealBox = new Rectangle(5, 10, 20, 30);

Edible e = null;

Which of the following assignment statements are legal?

  1. e = sub;

    Since the Sandwich class implements the interface
    , this is no problem. We can say
    e = sub
    with no issues. It Works

  2. sub = e;

    Since we are trying to change our object
    in the
    class to an
    type, we can't do this without casting. It won't work

  3. sub = (Sandwich) e

    Works! This fixes our old problem

  4. sub = (Sandwich) cerealBox;

    I have no clue.. but it should work?
    is a
    so with
    we convert it to
    , which is part of

  5. e =cerealBox;

    Don't think so.
    isn't implemented the
    interface so it shouldnt work

  6. e = (Edible) cerealBox;

    Should work now. (Edible) acts as if it implements the interface.

  7. e = (Rectangle) cerealBox;

    Not sure. I don't think this will work, I mean
    is of type Rectangle, why are we making it a

  8. e = (Rectangle) null;

    Not sure at all

Answer Source

#4, 6, 7, & 8 are the ones you need help on. They are all about casting. A cast is only legal if the origin type and the destination type potentially have a relationship through inheritance. A variable assignment is legal only if the right-hand side has the same type or a subtype of the left-hand side.

#4 sub = (Sandwich) cerealBox;

Both sides are of type Sandwich after the cast, however (Sandwich) cerealBox is an illegal cast. This will not work. The reason this is an illegal cast is because casting cannot happen between classes that are on different inheritance branches. There is no situation where a Rectangle could be a Sandwich, so this is a compiler error.

#6 e = (Edible) cerealBox;

This is different from #4 because it is an interface. It is legal, but may throw a runtime error. See this question for a good explanation. At compile-time, there's no way to know if cerealBox might actually be a subtype of Rectangle that is Edible. This will work. If Rectangle was final, this would not work.

#7 e = (Rectangle) cerealBox;

It's legal to cast a variable to the same type. It is not legal to assign a Rectangle to a variable of type Edible. Rectangle does not implement Edible. This will not work.

#8 e = (Rectangle) null;

It's legal to cast null to any type. This will not work though because Rectangle does not implement Edible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download