aspose file tools*
The moose likes Java in General and the fly likes overloaded func, both accept same no and type of parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "overloaded func, both accept same no and type of parameters" Watch "overloaded func, both accept same no and type of parameters" New topic
Author

overloaded func, both accept same no and type of parameters

Jigar Gosar
Ranch Hand

Joined: Jul 09, 2002
Posts: 95
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.


Regards,<br />Jigar Gosar (SCJP 1.4)<br />visit: <a href="http://jigar.org/blog" target="_blank" rel="nofollow">http://jigar.org/blog</a>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
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 ]

Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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....



SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jigar Gosar
Ranch Hand

Joined: Jul 09, 2002
Posts: 95
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 ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Jigar Gosar
Ranch Hand

Joined: Jul 09, 2002
Posts: 95
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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Nov 07, 2003
Posts: 4982
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

Joined: Jul 09, 2002
Posts: 95
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

Joined: Nov 07, 2003
Posts: 4982
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

Joined: Jul 09, 2002
Posts: 95
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

Joined: Feb 27, 2002
Posts: 8919

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jul 09, 2002
Posts: 95
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.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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).


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jul 09, 2002
Posts: 95
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

Joined: Aug 20, 2001
Posts: 1817

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

Joined: Jul 09, 2002
Posts: 95
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.





 
 
subject: overloaded func, both accept same no and type of parameters