Reputation: 18128
i am trying to create a interface where its methods are protected or only visible to the class that implements it.
The issue is this. i have two classes that do more or less the exact same thing but with different parameters and behaviour, but the actual steps it takes is identical.
i was thinking ok i have two similar classes so lets just create a interface that both these classes implement. Should be ok right? well it is. it does the job but with a serious flaw, All and i mean all of the methods it overrided in these two classes are now visible to the naked eye.
They can be accessed as they are defined "public". there goes encapsulation :(
Another solution i thought of was using a abstract class but wait, you can only extend one abstract class and the two classes i am using already extend a class(in my case a android Service class) so out goes that idea too.
Upvotes: 1
Views: 1573
Reputation: 55957
I'm not sure that your problem statment is right:
i was thinking ok i have two similar classes so lets just create a interface that both these classes implement. Should be ok right? well it is. it does the job but with a serious flaw, All and i mean all of the methods it overrided in these two classes are now visible to the naked eye.
The interface you define is the public "face" of the implementation classes. There is no requirement that there be anything in the Interface other than the methods you want to expose to the callers, and these can reasonably be public.
So what do you mean by "all of the methods it overrided" Interfaces don't override anything, so what's the "it" in your sentence. I don't see how using the Interface exposes anything that doesn't need to be exposed. In concept marking the Interface protected would just mean that the class itself can see the methods. I don't see how that's much use.
Perhaps you are concerned about what is exposed by the Interface. The key question is who is using that Interface. You can declare the Interface with default access (neither public nor private) and hence restricted to classes in the same package.
Upvotes: 0
Reputation: 46
IF both of your classes are extending service class better you declare a abstract super class that will instead extend Service class. So you see you need not worry about the inheritence structure as well as visibility of your methods.
Upvotes: 3
Reputation: 421310
That's correct. An interface
is a definition of the classes interface, thus you shouldn't be worrying about implementation details, such as how the interface is implemented. Not even in terms of protected methods.
In most cases this is solved by using an abstract class. If your two implementations already extend two other different classes, then you may have to "repeat" yourself in the code a bit.
Though I'm not sure I understand your issue here. When you say that the classes behave the same in many ways, I assume that you want to write this behavior in a common super class, right? But if you want to put it in a common super class, the two classes must extend this class. In that case, you could simply do this super class abstract, as you describe, no? I mean even if an interface could declare protected methods, you still wouldn't be able to share the common implementation parts in this interface.
Upvotes: 3