• Post Reply Bookmark Topic Watch Topic
  • New Topic

Design question: interface access  RSS feed

 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So.

I have an interface and a set of classes that implement this interface. It occurs to me that in these classes are some methods I'd like to fire off using the interface, but there's no need for everyone to be able to do this.

But every method in an interface is implicitly public, so there's no way to encapsulate these methods. Why is that? Do people just use abstract classes instead of interfaces in this case? What is behind the decision to force everything in an interface to be public? Am I missing some "trick"?
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can code the empty-method, like this:



It's very ugly, but it works
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I'm fully understanding your problem. Can you give a simple code example?
 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure. here's a simple example:



this, of course, will not compile because interfaces and all their members are implicitly public. any class implementing an interface method must provide public access for that method.

my question is why is that so? doesn't it seem like protected or default access would be appropriate? in such cases do people usually just use an abstract class instead? so maybe i have the wrong idea of the common purpose for interfaces...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!