• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extending class using fields/variables  RSS feed

 
Michael Carroll
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a generic class that delivers my functionality. I then extend this class and just pass in configuration. However, I would like this configuration to be in the form of fields/variables, as opposed to setter methods. I'm wanting to do similar to the following;



But I only seem to be able to get the following to work, using setter methods;



Anyone have any thoughts or ideas as to how I can get the first option to work? I'm no expert with java, so if it's not possible/goes against OO principles then that's cool too.

Thanks.

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The option that's working for you is definitely better style; but you can get it to work the way you want by declaring the variable in the base class, and setting the value in the subclass:



That override of processRequest() in class "a" is totally unnecessary, by the way.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Since you are new, I have edited your post and added code tags; you can see how much better it looks
 
Michael Carroll
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great stuff - thanks Ernest.

Cheers.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to reiterate what Ernest said, this isn't particularly robust or safe.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
really great..........i like it
 
Michael Carroll
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, I've taken onboard what you guys said, and am sticking with the setter methods.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!