aspose file tools
The moose likes Beginning Java and the fly likes why can't a class make an interface method private? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "why can Watch "why can New topic
Author

why can't a class make an interface method private?

Steve Harmison
Greenhorn

Joined: Sep 01, 2007
Posts: 4
All methods of an interface must be public. And if the method must remain public in a class that implemets it, then that is bad, because we might not want it to be public!
So what is the reason behind Java not allowing interface methods to become private inside a class?

Thank you.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1176
    
    3

Because the purpose of interfaces is to publicly declare a behaviour that a class has, therefore it doesn't make sense to be able to restrict the access to the method.

If you do need to do this then it's most likely that you have a design issue to resolve. Maybe splitting the interface into two (or more) parts and possibly having 1 extend the other would help. That way your class only needs to implement the interface(s) that defines the behaviour it has.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A couple times I found myself wanting to force a subclass to implement some method that really ought to be private. Is that what you're looking to do? Java won't let us, and it turns out it doesn't make much sense. You have to give implementers the freedom to implement any way they please.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Steve Harmison
Greenhorn

Joined: Sep 01, 2007
Posts: 4
Thanks everyone.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Oh, I probably read you wrong. I was thinking you meant to put a private method in an interface, but maybe you meant for the implementing class to make a public interface method private in the implementation.

The interface is often called a contract, which just means it makes promises to clients that must be kept. If I write a method that depends on an interface, I rely on the promise that all the interface methods will be available:

size() has to be there for all implementations of List. If you could write a new List implementation that made size private, and if you passed it into my method, what should happen? There's no good answer, so the compiler just doesn't let it happen. (Ok, there are some answers we only have to look at loosely typed languages to see, but Java isn't one of em.)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: why can't a class make an interface method private?
 
Similar Threads
Interface
Questions on for Kathy Sierr or Bert Bates
Abstract Method
What is "encapsulation"? How would I describe "encapsulation" in relation to a variab
How to get ServletContext in struts 2 action class