Skip to content
Prev 23920 / 63424 Next

S3 vs S4 classes

On Sat, 17 Feb 2007, Rossi, Peter E. wrote:

            
For new projects, perhaps: the issue is far less clear cut for existing 
ones.  In particular, the structure of model-fitting classes inherited 
from the White Book (lm, glm and so on) is S3 classes, and so related 
model-fitting functions (e.g. my polr) fit most naturally into the S3 
paradigm.
S4 classes should use show() rather than print(), according to the 
established practice (and I think the Green Book, but I do not have it to 
hand).  Certainly auto-printing uses show() for S4 objects in preference 
to print().
If you define an S4 method for any function, e.g. plot, it becomes S4 
generic and after looking for S4 methods will use the previous function 
(and if that is S3 generic, it will still look for S3 methods).

To my mind the advantage of S4 classes is that they are what JMC sometimes 
calls 'formal' classes: they are rigidly defined.  That suits them to new 
projects where you can think about the classes you want and define their 
structure and relationships in advance.  On the other hand, for 
development and experimenting that rigid definition can be a 
straightjacket.  I have moved various model-fitting functions to S4, and 
moved them back to S3 in frustration at the lack of fit to existing tools.