• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Modifiers For The GUI

 
Simon Quirke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Again JavaRanch,

In my Bodgitt and Scarper GUI I create the main JFrame in the Constructor which in turn calls a number of methods to create and populate various panels. Some of the method signatures are given below.



Most of the methods depend on instance variables such as:



My question is what access modifiers are the most appropriate for a Swing GUI? Should I specify everything as protected so my classes could be extended at a later date or is default more appropriate.

Thoughts and comments appreciated

Simon
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, this sort of translates to the question: What is the likelihood that my class will be reused?

If your class is reused you might *still* want controlled access to your instance variables by supplying getXXX() methods like getMenuBar, getMainTable, getNameField, etc...

Particularly, look at Swing as is... if I recall correctly, Swing doesn't allow access to its instance variables either.

But truth be told, for this project I'd just leave them private and not worry about inheritence so much. Unless you're building something like a specialized JTextField, in which case, yeah, g'head and publish accessor methods.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon,
IMO, one should not assume that his classes will be extended unless it were designed and documented for inheretance. If you intentionally designed your classes and document it for inheretance, then it should be public or protected. However, if you didn't designed it for inheretence, then your classes should be private of default modifier.
Inheretance can produce bugs if it is used inproperly. I always favore Composition and Forwarding over Inheretance.
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's forwarding?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic