I have a class with 5 methods. 3 of these methods just have to be opened by other classes in the same package and 2 have to be opened by other classes in an other package.
as example:
void setTimeArray(int[] zeitArray) {
this.timeArray = timeArray ;
}
public int[] getTimeArray() {
return timeArray ;
}
now I was wondering what I should make:
- should I make the 3 methods
protectedand the 2 otherpublic?
or - should I make an interface for the 2 methods?
so what would be cleaner and better for the performance of my application and why?
You seem confused about the use of
public,protected, etc. The public methods in your class comprise the public interface of your class. When you design your class, you decide what functionality you want to expose to consumers of your class.You should only make methods protected IMO for polymorphism. If you are making a method protected so that another class in the package can get at internals, etc., then it is probably a bad class design. You should not make a method protected just because no other classes are using it right now. If you need to use it from another class in the future, you’d have to change the class.
You shouldn’t need to create an interface if there aren’t multiple concrete classes implementing that interface.
The public interface of a class should flow pretty naturally if you get the OOP paradigm. The decisions should involve more about how to expose function than what to expose.