This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

implementing method doStuff() in class must be marked public

 
Greenhorn
Posts: 19
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: 1248
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: 1248
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: 69740
277
  • 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?
 
Ranch Hand
Posts: 62
  • 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.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic