This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

implementing method doStuff() in class must be marked public

 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently studying for the OCA exam. I just saw that question on on a mock exam.

Given:




The answer was Compilation fails because the implementing method doStuff() in class MultiInt must be marked public. I do not understand why. Could anyone please explain this to me ?


 
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always QuoteYourSources  ( name of the book with author(s) name or website URL ) to avoid copyright infringement.
 
Ganesh Patekar
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the rules of method overriding is: The access modifier of an overriding method must provide at least as much access as the overridden method ( default method of interface ).

Implicitly the access modifier of default method is public but in MultiInt class overriding method int doStuff() has package-private access modifier which is weaker or lesser accessible than public hence the compilation error.
 
Marshal
Posts: 65072
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another version of what Ganesh has told you:-
Implementing an interface has to follow an IS‑A test rather like for inheritance. If you have an instance of interface I1 and you are using it from a different package (e.g. public class Foo implements I1 { } ), it has a doStuff() method. Don't cert exams allow dreadful names for methods!
If you have an instance of MultiInt and you try to us it from a different package, it doesn't have a visible doStuff() method, so you would think that a MultiInt ISN'T‑AN I1. So MultiInt must have public access for all its methods inherited from the two interfaces.
By the way, what will happen if you omit the doStuff() method declaration from lines 11‑12?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example

Interface Test.java



Test.class Profile



All abstract, default and static Methods in interface are implicity public, so you can omit the public modifier.
 
Campbell Ritchie
Marshal
Posts: 65072
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NS: Please avoid all bold text; I have changed that to normal text.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!