Results 1 to 4 of 4
- 10-08-2010, 03:35 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Pass multiple object types to one function
Hi all,
I have a requirement where I have to pass a number of objects to a function and process them by calling a common method, getComments().
I would like to pass any of a number of types of objects, call the getComments() method, parse, then update the original object.
You can think of something similar like passing objects like:
cars, trucks, motorcycles and calling a method like getCurrentValue() (or something similar).
Is this possible?
Thanks much in advance!
M
- 10-08-2010, 03:50 AM #2
If you have a class Car, a class Truck, and a class Motorcycle, they could all extend a Vehicle class which contains the methods you want. Each of the subclasses can then override those methods and the Vehicle object can be safely passed to the method.
- 10-08-2010, 04:12 AM #3
Or you could have all the classes implement an interface (for discussion's sake let's call it Commentable) and the method (not function, Java doesn't have functions) wold accept a Commentable argument.
A getComments method shouldn't update anything. By convention, getXxx methods are accessors. If the purpose of the method is to update the comments, it should be named updateComments. If simply to set a new value for the comments field, then setComments.call the getComments() method, parse, then update the original object.
And if the method obtains all input via its parameter(s) and doesn't depend on the state of the current object, it should probably be static.
db
edit Oh, I think I misread that. The method that does the updating isn't getComments.Last edited by DarrylBurke; 10-08-2010 at 04:30 AM.
- 10-08-2010, 07:51 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Sorting/Searching Objects with multiple types.
By gcampton in forum New To JavaReplies: 20Last Post: 10-21-2009, 11:58 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Multiple types in Vector - type checking
By Excession in forum Advanced JavaReplies: 5Last Post: 07-13-2008, 07:06 PM -
Hastable - Multiple Types
By sopna sajith in forum Advanced JavaReplies: 3Last Post: 06-29-2008, 04:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks