It's not a secret anymore!
The moose likes Beginning Java and the fly likes Type Checking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type Checking" Watch "Type Checking" New topic

Type Checking

jesse taron

Joined: Sep 13, 2003
Posts: 2
I have a method that adds an object I have created (car) to an ArrayList. I have been told that the method used to add the object should return a boolean so to allow type checking to make sure only car objects get added to the list. I am not sure how this is beneficial because if the method declaration is:
public boolean addCar(Car myCar){
then if an object that was NOT a car was passed into the method, the compiler would catch it anyways.
I hope the above is clear, thanks for your help.
AJ Keats

Joined: Sep 27, 2003
Posts: 2
One thought occurs to me, but I am not sure that its applicable to your situation. But here goes.
Using your example, a Car reference can also refer to an object of any class derived from Car. For example, if you create a subclass of Car called SportsCar, then you could pass a SportsCar to addCar(). Perhaps you don't want to allow this for some reason?
I agree. Here's the link:
subject: Type Checking
It's not a secret anymore!