If your class extends a super-class, then you obviously will not be able to reduce the scope of any methods. Should you widen the scope of any super-methods? Probably not.
What about non-overridden methods? If the method is defined by an interface, then it must be declared public -- end of story. But should any other methods be public? Well, let's think about how methods in our class will get invoked. One mechanism of invocation is via reflection, in particular, introspection. Introspection looks for public methods with names of the form:
- boolean isX() and its mate setX(boolean);
- T getX() and its mate setX(T).
If you have these reflectible methods or other non-overridden methods that you want to be invoked from other classes by the normal calling mechanism, then you need to declare them public and, if the method receiver type will normally be an interface, then they must go in the interface.
What this implies is that, for a class that will normally be referenced via its interface (thus appropriately hiding the specifics of any concrete class), the only public methods in that class will be the bean methods and the interface methods. No other method needs to be declared public because there will be no way to invoke it.
Meanwhile, what about protected and default scope? I tend to use protected scope only for methods that are used internally (that's to say they are invoked by a base class) and declared either abstract or with a trivial default behavior and which are expected to be overridden by concrete classes to define class behavior. Occasionally, I will define a concrete non-overridable method as protected when I am sure that I only want it to be invoked by sub-classes. And, typically, the only need for default scope is when you create an inner class within a class and you want to create a method which will allow communication between an inner class and its outer class.
Everything else should be declared private. I like to use long names for private methods which thus give a good description of what the private method does (naturally, it only does one thing!). I do not create javadoc annotation for private method or fields because I typically filter out all private objects from the resulting javadoc.
I also feel that private methods should not normally handle any exceptions. Exceptions should be handled at a level where handling is either required or opportune. Therefore, private methods may have a long list of thrown exceptions, as required.
And while good practice suggests keeping short parameter lists for public methods, private methods can use as many parameters as they please.
Finally, a word on the other method modifiers, apart from scope. Marking individual methods as final is unusual, but is necessary in certain cases, for example delegated callback methods. And what about the distinction between class methods (static) and instance methods? I have the Java compiler configured to warn me about methods which can be defined as static, but aren't. This is very useful and something which I have long looked for. I feel that declaring a method as an instance method when it really should be a class method is just plain wrong because it implies a dependence on this when none exists.
OK, back to work!
No comments:
Post a Comment