• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Defining setters as interface methods ?

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I misguided in defining setters as interface methods ?

e.g
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say it depends. For some interfaces, it might very well be appropriate to define setter methods. For some, it might not be.

Was that a real world example you posted? If yes, could you elaborate on the meaning of the interface and the objects involved?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't we define setter methods for a Mbean? :roll:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, of course setters are evil per se, but they don't get much worse by being put into an interface...
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm..

A setter is normally used to set a value to a variable (a structural attribute) on the class, right?
Also, an interface is used to define common behavior of multiple implementation classes, right?
Additionally, an abstract class is used to define common structure AND behavior of multiple implmentation classes, right?

Ergo, wouldn't you want to define an abstract class with the variable and an abstract setter method on the class the subclasses would actually implement and not an interface?
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to my code extract:-

Foo is actually a Runnable.
My intention is to allows users to substitute their own implementation of Foo. The framework will instantiate - once - the specified implementation of Foo during startup and for every requests that comes in, it will create a thread by passing it Foo.

Those setters in Foo will be called just "once" in the constructor of the bootstrap class for the framework. Comments ?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sathya Srinivasan:
Ergo, wouldn't you want to define an abstract class with the variable and an abstract setter method on the class the subclasses would actually implement and not an interface?



Well, if it defines the variable, the setter probably should be concrete, not abstract, shouldn't it?

But sometimes you don't know what the class will want to do with the set value, you only know that clients want to set it. In those cases an interface is the perfect choice.

As an example, I recently introduced the following interface in our system:


public interface ExplicitParentSetable {
public void setExplicitParentComponent(Component explicitParentComponent);
}

 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pho, your solution sounds good to me!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic