Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Overloading interface methods

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does Java allow interface methods to be overloaded?

Thanks,
Von
 
author and iconoclast
Posts: 24203
44
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
Yes, absolutely.
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I thought so.
 
Sheriff
Posts: 22120
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These kinds of questions can be easily answered by trying it out yourself with a little example.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you cannot do like this.


but this is perfectly legal..

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface and Overloading are quite independent mechanism; we should not try to mix them ..
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
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

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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@K Abhijit

Could you please use codetags while posting code.
Thanks
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------ignore this post please---------
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened to the code tags?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vonique Leary wrote:What happened to the code tags?



Have a look on this link => UseCodeTags <=. And you are correct!
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@shanky : done
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, that's right. I'm thinking html with the < and /> tags.

Thanks!
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic