GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Overloading interface methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloading interface methods" Watch "Overloading interface methods" New topic
Author

Overloading interface methods

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Does Java allow interface methods to be overloaded?

Thanks,
Von
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes, absolutely.


[Jess in Action][AskingGoodQuestions]
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Thanks, I thought so.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

These kinds of questions can be easily answered by trying it out yourself with a little example.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

you cannot do like this.


but this is perfectly legal..


SCJP6.0,My blog Ranchers from Delhi
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
interface and Overloading are quite independent mechanism; we should not try to mix them ..


“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

K Abhijit wrote:interface and Overloading are quite independent mechanism; we should not try to mix them ..


I completely disagree. There's absolutely no reason why an interface shouldn't have overloaded methods. There are a number of such interfaces in the Java API; java.io.DataOutput has three overloaded write() methods, for example, and java.util.Collection has two overloaded toArray() methods. It's a perfectly fine thing to do.

Shanky Sohar wrote:you cannot do like this.


Well, indeed, you have to obey the rules of the Java lanaguage. Neither of these examples is what the original poster was asking about, though. If an interface includes both "Object[] toArray()" and "Object[] toArray(Object[])", then a class implementing this interface has to implement both overloaded methods, of course.
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
@Ernest Friedman-Hill

I think you didn't get me right ... what I meant was something else....

When i said these 2 things are quite independent that DOESN'T mean that they can not co-exist...
it should be considered as OVERLOADING DOESN'T HAVE ANY IMPACT ON INTERFACE AND VICE VERSA....


Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@Ernest Friedman-Hill

I was little confused.
So i presented both the thing that what can we do and what we cannot.
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
Ernest Friedman-Hill wrote:Neither of these examples is what the original poster was asking about, though. If an interface includes both "Object[] toArray()" and "Object[] toArray(Object[])", then a class implementing this interface has to implement both overloaded methods, of course.


I think we all didn't get his original questions here.....

if interface itlsef contains Overloaded methods then this question is absolutely USELESS ..the entire Java Api is based on interfaces full of overloaded methods....

What he was trying to ask is: (this is a bit guess and Post Creator should correct me incase i'm wrong)


I think he is trying to ask if void doFoo(int f){} ..... is allowed or not...

Ans is Yes.. perfectly legal...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@K Abhijit

Could you please use codetags while posting code.
Thanks
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
The reason why we have a plethora of answers all with different opinions is because the question is not clear. Vonique please be more specific and provide more description about your doubt - especially when there are different ways in which a question can be interpreted, else you can see what happens


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

---------ignore this post please---------
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Well, the reason I asked is because I am studying out of SCJP Sun Certified Programmer for Java 5 book and one of the questions is this:

<code>Given:

public abstract interface Frobnicate { public void twiddle(String s); }

Which is a correct class? (Choose all that apply.)

A. public abstract class Frob implements Frobnicate {
public abstract void twiddle(String s) { }
}

B. public abstract class Frob implements Frobnicate { }

C. public class Frob extends Frobnicate {
public void twiddle(Integer i) { }
}

D. public class Frob implements Frobnicate {
public void twiddle(Integer i) { }
}

E. public class Frob implements Frobnicate {
public void twiddle(String i) { }
public void twiddle(Integer s) { }
}

17.Given:

class Top {
public Top(String s) { System.out.print("B"); }
}</code>

and the answer involved this comment:
"D is incorrect because overloading a method is not implementing it."

So, I mistakenly assumed that interfaces do not allow overloading, but now I realize what they are saying is that the method may be overloaded as long as it is also implemented in its original form.

Anyway, hope that clears things up.

Thanks,
Von
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
What happened to the code tags?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vonique Leary wrote:What happened to the code tags?


Have a look on this link => UseCodeTags <=. And you are correct!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
@ Von: use [ code ] [ / code ] instead like this..... ( to see actual message body try quoting ) ..


Which is a correct class? (Choose all that apply.)

A.

B.

C.

D.

E.


17.Given:



and the answer involved this comment:
"D is incorrect because overloading a method is not implementing it."
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
@shanky : done
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Oh, that's right. I'm thinking html with the < and /> tags.

Thanks!
 
GeeCON Prague 2014
 
subject: Overloading interface methods