• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overloaded func, both accept same no and type of parameters

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have two funcs,

Record getRecordForCol1AndCol2(int col1, int col2);
Record getRecordFoeCol1AndCol3(int col1, int col3);

i wnat to use getRecord(int,int);
but i cannot overload them, cause both accept two ints , whats the solution.??? and i dont want to use long name describing columnNames.

actually i have couple of such overloaded functions and all of them accept two or three ints, but based on the column name and its value return different record. so whats the generic solution.

please help.

thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not understanding you. It sounds as though the arguments you pass to these two functions would be fixed -- i.e., the first one would always want 1 and 2, and the second one 1 and 3. So why not just have one method, and take action based on the values of the arguments with an "if" statement?
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jigar. It sounds like you don't really need overloaded functions unless the internal algorithm is different. If it is different, and if you must have overloaded functions, then I suggest converting the first of those parameters to a long. It's kind of a strange solution, though, because you would also need to convert the value to a long. So that will give you two overloaded functions total.
[ November 17, 2004: Message edited by: Anton Golovin ]
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if your both method are have same process, you can use only one method to solve your problem Because you don't need overloading method.




How to use....

 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, it is difficult to think of whether you need the same signature, but based on the input, you have different logic, and thus, you need to overload the method.

I think of a case that might let this happened.

We might have this:


However, you can solve it easily by renaming the methods to checkValidateAgainstProductType and checkValidateAgainstProductStatus.

Thus, if the logic is different, you could already differentiate the methods by their names.

Nick
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'll clearify the problem.

i have a Record class


and a





so now tell me i , how can i overload these methods , using a getRecord();

imaging if i have to take three parameters and the param names are not as short as f1,f2,f3???

what is the solution ??? cause i dont want to use long names like "getRecordForF1AndF3()"


please help me.
[ November 18, 2004: Message edited by: Jigar Gosar ]
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use getRecord(int j,int k, int whichF)

where which f1 represents either
a.f1 and f2
b.f1 and f3
c. f2 and f3

You could assign values 0,1,2 to whichF depending upon which f's you want to use.
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice one pradeep but it sounds clumsy to me, but any ways thanks for it, it is really a solution that i could not think of.


but is there any elegant solution where caller dosent have to bother, sending a extra dummy parameter.


thanks.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that the solution is not good . I can think of another simialr solution but it not good either.

I dont think you will have any better ones for the requirements you have.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, for common comparsion, you can isolate that part out. i.e. you do NOT overload the 3 methods, instead, these 3 methods will invoke the same processing method for the logic that can be shared.

It seems that it is better than you try to merge those methods, which in fact they are not really the same.

Nick
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nicholas Cheung, i dont get what r u trying to say???

should i mergethis methods , how???

should i overload these method, how???
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, you could:



Could you do that? In fact, if no special treatment on f1, f2 and f3, you can even combine these methods.

Nick
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no dude that cant be done, because that common method is only comparing r.x and r.y , not my Record has many feilds and f1 should be compared with r.f1 and so on,.....


and aneways that is not my problem , i m not asking how to reduce code here, that was an example so that i can ask this question???


HOW CAN I OVERLOAD METHODS that take TWO int PARAMS AND DO DIFFERENT STUFF................................
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

HOW CAN I OVERLOAD METHODS that take TWO int PARAMS AND DO DIFFERENT STUFF



It can't be done.

You could make one to method to accept int arguments, 2nd one Intgere args and the 3rd one Long arguments.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it. A programmer has to call your "findRecord()" method. He wants to call it with two numbers, 34 and 57. He knows, though, what sort of lookup he wants to do, right? I mean, he knows that 34 is the f2 value and 57 is the f3 value. I don't think there's any reason why he would object to having to express this knowledge; i.e., don't overload the methods, but encode the purpose of each method in its name.

On the other hand, Pradeep's suggestion (which he meant as a joke, I think) leads you to another solution that does allow overloading; very crudely, it looks like this:



Then you can call

RecordManager.getRecord(new F1(34), new F3(57))

and get the right overload. But personally, I'd prefer the non-overloaded version myself!
[ November 18, 2004: Message edited by: Ernest Friedman-Hill ]
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another gr8 solution.....

something that didnt pop im my head.

it feels gr8 to share such ideas of other ppl.

creating a wrapper is another non-elegant solution that works. but a nice one.

as u said pradeep's earlier solution is better, in which we pass another parameter, conveying our intension,

but then it will be a class constant, like int public static final Record.FOR_F1_AND_F2,

but then it is better to use a more descriptive name for getRecord() function.


so i agree using an overloaded func is not a good idea.

but now the problem is how to name these functions;

as u ppl know these functions return a record based on two ints, where the two ints are member variable of class Record.

now, how do u suggest i rename this method,

getRecord();

if the feilds in Record are

Record
{
int entityTypeId;
int actionTypeId;
int actionStatusId;
int prevActionStatusId;
}

getRecordForEntityTypeIdAndactionTypeId (int entityTypeId ,int actionTypeId){}

getRecordForEntityTypeIdAndActionStatusId (int entityTypeId ,int actionStatusId){}


these names are too long to be used???

so what do u ppl suggest ??? how should i rename getRecord() function???

since i didnt find a good way to rename it , i thought of overloading. but since overloading is not possible, how do i name these functions.???

i m sure there is an elegant solution.

so please help me with it.

thanks.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand, if your member variables are public, you could specify four parameters:


This is a little bit more flexible than specific methods for each possible combination. However, the fields have to be public (and the security manager has to allow it).
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jigar Gosar:


as u said pradeep's earlier solution is better, in which we pass another parameter, conveying our intension,



Actually, I never said that. I think that's a horrible solution. I was talking about the fact that Pradeep pointed out that to use overloaded methods, you have to use different parameter types.
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok good now things are clear,

but can u help me in naming these functions, or use other solution to achive same effect???
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another solution, which I think is slightly better than my earlier suggestion using reflection. This one creates a RecordSearch class, and the the Record simply checks itself agaist the record search class. Quite simple. You could either construct the RecordSearch objects the way I did in the code, or (more likely, especially if there are more than 5 fields), take a couple of lines and build it how you see fit, like

Anyway, here's the code to do this:

[ November 18, 2004: Message edited by: Joel McNary ]
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
third mind blowing solution of the day.

i am really surprised that u have gone to such gr8 lengths to convey ur point.

thanks a lot for it.

but my problem is :

valid searches are "f1,f2" "f1,f3" and "f2,f4" only rest all searchs are invalid in the first place,

do if i do something like

recordSearch.f1=10;
recordSearch.f4=20;

and then call getRecord() then this call should fail. cause business logic doesnot allow to fetch a record only "f1,f4"

so now inorder to restrict i'll have to add the check, which will throw a runtime exception, if arguments are not correctly specified.

i'll have to document the correct permutations , which is worst than long function names.

so the better idea will be

getRecordForEntityTypeIdAndactionTypeId (int entityTypeId ,int actionTypeId){}

getRecordForEntityTypeIdAndActionStatusId (int entityTypeId ,int actionStatusId){}

i get all the compiletime benifits, no need to provide rules for using getRecord () method correctly, no need to throw an exception.

the only problem is function name is too long and what if i have a function that takes three parameters.

another option will be to use codes like

getRecordETAT(int entityTypeId ,int actionTypeId){}

getRecordForETAS(int entityTypeId ,int actionStatusId){}

and parameter names will be my documentation. a clue to client what parameters to pass.

EUREKAAAAAAAAAAAAAA.........

i finally found a semi-elegant solution to my problem , which is quick to write, and gives me all the benifits.

thanks to all for taking time to reply to my queries.

thanks a lot.





 
reply
    Bookmark Topic Watch Topic
  • New Topic