With solid C++ in my back some Java concepts are not 100% clear.
List<SomeInterface> list = new ArrayList<SomeImplementation>;
This is 100% clear needs no explanation - but it has consequences...
interface Drivable {...}
class Car implements Drivable {...}
class Bike implements Drivable {...}
class Skateboard implements Drivable {...}
class MyGarage{
List<Driveable> myRides;
public void addRides( List<Driveable> rideList ){...}
Is not callable with List<Car>
. However I can add Cars to myRides by:
public void addCars( List<Car> carList ){
myRides.addAll( new List<Driveable>( carList ) );
Q1: Is this a good idea?
I would really want something like the first function to work. By doing something that corresponds to C++ const&
and adding a copy of the incoming carList to myRides I am thinking carList would be protected from illegal access. My hopes of adding final
as a function argument were crushed.
Q2: Is there some way to make a fully callable addRides(List<Driveable> rideList)
work?
Nor is overloading ( List<Car> List<Bike> etc
) legal.
And yes I can make this work by adding separate functions addBikes() addCars etc
or I could do new List<Drivable>
at each point of call. But I am hoping for something sleeker.
Q3: Any suggestions?
Thanks a lot! Adam