Subtyping and assignment in Java

Posted by Danrex on Stack Overflow See other posts from Stack Overflow or by Danrex
Published on 2014-06-10T03:21:48Z Indexed on 2014/06/10 3:24 UTC
Read the original article Hit count: 57

Filed under:
|

Arghh I just know people are going to hate me for asking this...

I was just playing around with inheritance and I noticed you can instantiate a subclass object in one of two ways when you write code. So then I wondered if there is any functional difference between these two methods. So in the code below, does this produce the exact same result...a MountainBike object, or is there some difference I should know about? Bicycle is the superclass for this example. If I do Bicycle bike or MountainBike bike I am effectively making a MountainBike due to new MountainBike()? So basically the difference is just semantics at this point?

 Bicycle bike = new MountainBike();
 MountainBike bike = new MountainBike();

© Stack Overflow or respective owner

Related posts about java

Related posts about inheritance