Re: Beginner doubt about access and methods
Jordi wrote:
These methods may only be used by classes in its package
I avoided using public methods or public variables for this class and
its subclasses to increase security.
Is this a good point, or not?
So far, so good. Although for instance variables I prefer private.
> So when I write a class that extends the class Data, I must set those
> variables with:
> setData("blabla");
>
This I don't understand. Usually, an abstract class has abstract
methods. If all you are doing is setting instance variables, I think
you may need a regular class.
class MyData {
private String someData;
void setData( String s ) {
someData = s;
}
public static main( String [] args ) {
MyData datum = new MyData();
datum.setData( "blah" );
}
}
This is fine.
I saw some people do things like this but I don't know if I am doing
well. Most people used private variables but public methods.
Package private is fine, if that's what you want. I think it's a good
way to add structure and encapsulation to a package, while preventing
classes outside the package from getting at classes which they
shouldn't. I do prefer private instance variables, since I think that's
also good encapsulation.
My only concern is that I don't see why the class needs to be abstract,
and I don't see why it needs to implement Serializable. (You don't do
anything with Serializable in your example.)