• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Protected Methods in Interface

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One very good oops question I faced first time, and I did not realize the answer at that time in the interview, but I think this is a very good question for scjp concept.

Why one cannot put protected methods in an interface?

Answer is really very easy if one understands working of protected keyword, but I myself did not realize, let us discuss this, please put your inputs here.
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that will defeat the purpose of code to interface. Protected members are only available to sub-types of the type, so if interface methods are protected, they cannot be called outside of that type's hierarchy. And thus we would be forced to use the concrete class types instead of using interfaces as object references...
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Punit,

The methods of interface are always public and abstract if not mentioned they are taken implicitly by the compiler if we put any other access modifier like final ,static private or protected then the program will not compile.
 
You've gotta fight it! Don't give in! Read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!