• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Nikos Blog Overloading Question 58

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will this code compile successfully? (1 correct answer)


1. Yes.
2. No.

Niko says b, but it compiles fine for me, where is the problem? I thought you can add as many overloading methods you wish, as long as the attributes differ. Or is it just because the import statement lacks for the Overload class?
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks correct, however I get the following compiler error:

Overload.java:10: method(java.util.NavigableSet<java.lang.String>) in Overload cannot implement method(java.util.NavigableSet<java.lang.String>) in Interface; attempting to assign weaker access privileges; was public
void method(NavigableSet<String> set) throws Exception {
^
1 error


However, if I make void method(NavigableSet<String> set) throws Exception a public method, then it compiles fine.



This is also confusing to me. Unless interfaces are automatically public and any method that overrides them must be marked public.

Please clarify

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,
Interface methods are by default public,abstract whether you declare it in interface or not.
In above code,when you are provide implementation to above public abstract method of interface in other
class that implements it,then it's access level and exception can't be more restrictive.(Overriding also says this)
default is more restrictive than public.
default can be made public or protected but vice versa is false.
 
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
The original code won't compile, because you breach the overriding rules!

Sandra Bachan wrote:
This is also confusing to me. Unless interfaces are automatically public and any method that overrides them must be marked public.



Seems to be correct!, But a small correction, Interface methods are implicitly public, the methods overriding them must be marked public(restriction rules)!

Arjun, What about first post? Does that compile?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:
Arjun, What about first post? Does that compile?


i don't think so,it doesn't compile.as default is more restricted than public.
correct me if am wrong.
 
Abimaran Kugathasan
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

Anton Brass wrote:
Niko says b, but it compiles fine for me, where is the problem?



That's why, I asked! It won't compile! Don't use Sir here, Please!
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make the overriding method PUBLIC
 
Anton Brass
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, my Eclipse added public automatically so. With the explanation it makes totally sense
 
reply
    Bookmark Topic Watch Topic
  • New Topic