public class CastingExample{
public static void main(String[] args){
Printable p = null;
BlackInk black = new BlackInk();
p = (Printable)black;
}
}
class Ink {}
interface Printable{};
class ColorInk extends Ink implements Printable{}
class BlackInk extends Ink{};
I dnt understand why there is no compilation error while casting black to Printable?? Instance of BlackInk cannot be Printable because it doesnt extend Printable interface.
It is true that an instance of BlackInk cannot be Printable because it doesnt extend Printable interface. But the variable black can point to an instance of a subclass of BlackInk and that subclass may implement Printable interface. So the compiler has no option but to allow it.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
So from what I understand, the compiler is not concerned with the right part of the assignment is which is ' new BlackInk ()' ? It only wants to know that BlackInk black can be assigned anything?
E.g.