• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type Checking

 
jesse taron
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){
list.add(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.
Jesse
 
AJ Keats
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic