R
Robert Elliot
Just wondering if the Java Language gurus ever gave thought to another
visibility modifier to go alongside public, private and protected;
something along the lines of subclass. Idea being that methods and
classes marked subclass would only be visible to instance methods of
the class and instance methods in subclasses of the class.
The reason I'd like that is not to protect me from being really,
really stupid and referencing a method or field I shouldn't be
referencing from another class in the same package; rather it's
because this visibility modifier could be over-ridden to private in
sub-classes, since it could never be called externally anyway. This
would mean that in a class hierarchy Foo -> FooExt -> Bar you could
have a method on Foo subclass visible to FooExt so that FooExt
instance methods could call it, but over-ride it in FooExt to private
so that Bar methods could not call it.
You can achieve something similar by over-riding the method in FooExt
and putting @deprecated in the Javadoc, of course, but this would be
more conclusive and would mean that dev environments like Eclipse
would not show the methods as available to the subclass, making it
easier for someone to learn the new API.
Practical idea, or dumb and unnecessary? Just that I'm writing an API
where the lack of this means that when the client subclasses FooExt,
as I intend them to, they get access to a bunch of methods on Foo that
FooExt needs but that I don't want the client to have access to; they
are no benefit to them, and their presence is confusing.
Rob
visibility modifier to go alongside public, private and protected;
something along the lines of subclass. Idea being that methods and
classes marked subclass would only be visible to instance methods of
the class and instance methods in subclasses of the class.
The reason I'd like that is not to protect me from being really,
really stupid and referencing a method or field I shouldn't be
referencing from another class in the same package; rather it's
because this visibility modifier could be over-ridden to private in
sub-classes, since it could never be called externally anyway. This
would mean that in a class hierarchy Foo -> FooExt -> Bar you could
have a method on Foo subclass visible to FooExt so that FooExt
instance methods could call it, but over-ride it in FooExt to private
so that Bar methods could not call it.
You can achieve something similar by over-riding the method in FooExt
and putting @deprecated in the Javadoc, of course, but this would be
more conclusive and would mean that dev environments like Eclipse
would not show the methods as available to the subclass, making it
easier for someone to learn the new API.
Practical idea, or dumb and unnecessary? Just that I'm writing an API
where the lack of this means that when the client subclasses FooExt,
as I intend them to, they get access to a bunch of methods on Foo that
FooExt needs but that I don't want the client to have access to; they
are no benefit to them, and their presence is confusing.
Rob