2

I don't understand why the casting from bellow doesn't work ( Helicopter h = (Helicopter) new Rotorcraft();) and throws a Runtime exception of type ClassCastException.

Base class:

public  class Rotorcraft {

protected final int height = 5;
    protected int fly(){
        return height;
    }
}

Child class:

public class Helicopter extends Rotorcraft {    
    private int height = 10;

    public int fly() {
        return super.height;
    }

    public static final void main(String[] a){
        Helicopter h = (Helicopter) new Rotorcraft();
    }
}
Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
SocketM
  • 564
  • 1
  • 19
  • 34
  • 8
    A `Rotorcraft` is **not** always a `Helicopter` – Eran Oct 09 '18 at 06:42
  • True but if I'm saying: trust me and and run the code, however it fails at run time. After casting I want to run the method fly on Helicopter reference. – SocketM Oct 09 '18 at 06:44
  • 1
    @SocketM "Trust me" the compiler did. And you didn't give it an instance of `Helicopter`, so the cast fails. – Andy Turner Oct 09 '18 at 06:45
  • You have it wrong in your mind. as you wrote you are saying that a helicopter is a rotocraft and thats why you are getting the error. instead, Rotocraft should extend Helicopter class. then the 'Helicopter h = (Helicopter) new Rotorcraft();' will work – Sir. Hedgehog Oct 09 '18 at 06:45
  • Why am I wrong, if I have the fly method in parent and subclass? This is not polymorphic behavior ? – SocketM Oct 09 '18 at 06:47
  • 1
    @SocketM a reference of type `Rotorcraft` can point to a `Helicopter` (or null), which is why you are allowed to do the cast. But if it points to a `Rotorcraft`, not a `Helicopter`, you can't simply pretend that it *is* a `Helicopter`, because `Helicopter` might (now or in the future) have methods amd fields not present on a `Rotorcraft`. If you want to make use of polymorphic behaviour, use it as a `Rotorcraft`. – Andy Turner Oct 09 '18 at 06:49
  • So basically when you do Rotorcraft r = new Helicopter() will always work becuase Helicopter object inherits from Rotorcraft. When we use a Rotorcraft reference for a Helicopter object we are using a subset of instance methods and fields available in Helicopter object. Vice versa when we try to use a Helicopter reference on Rotorcraft object we will get a ClassCasting exception due to the fact that Rotorcraft object is not an Helicopter. Rotorcraft object dosen't have all methods / instance fields that an Helicopter has or could have in the future. I got it right? – SocketM Oct 09 '18 at 07:09

1 Answers1

2

The basic issue is that you're trying to convert into a Helicopter something that isn't (it's a Rotorcraft). A cast cannot change the runtime class of an object.

Did you mean to simply write:

Helicopter h = new Helicopter();

?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Helicopter h = new Helicopter() will always work. I'm trying some polymorphic behavior :-) – SocketM Oct 09 '18 at 06:50
  • @SocketM: Polymorphism doesn't work like that (not in Java, anyway). You can't create an instance of a base class and then somehow pretend that it's an instance of a derived class. It isn't, and a cast won't make it so. – NPE Oct 09 '18 at 06:52
  • Thank you @NPE. Can you please check my assumption added on comments section attached to my question – SocketM Oct 09 '18 at 07:30