Subtyping and assignment in Java
- by Danrex
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();