1

Possible Duplicate:
What should the accessablity of Fields in a Abstract Class be?

Is it bad practice to use public fields in abstract classes? The reason I'm asking this is because when I inherit from an abstract class I cannot access a private field from the subclass, the only way(s) around this as far as I can tell is to either make the field public, or create get/set methods to access the field... Which practice is best?

Community
  • 1
  • 1
James Stevenson
  • 99
  • 1
  • 1
  • 6

3 Answers3

6

there are always protected fields

Peter
  • 5,728
  • 20
  • 23
6

Best practice is to use getters and setters, possibly protected or public.

If using getters and setters seems like over kill because all your implementations will be developed with the abstract class itself in the same package or module, you can use protected fields. I don't think its best practice but a pragmatic choice.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

In your case a protected member is propably the better choice as it will allow access to the member from within the class or any derived class.

As seen in this article on Member function visibility in Java programs. Article, includes a nice table for different Java accessors and when/why to use them.

Nope
  • 22,147
  • 7
  • 47
  • 72