R's OOP systems: S3 generic functions (print, summary, plot) dispatch on class attributes; S4 formal classes for stricter contracts (Bioconductor).
S3 class
fit <- lm(y ~ x, data = data.frame(x = 1:3, y = c(1, 2, 2)))
print(class(fit))
print(summary(fit))
Generic functions
summary() behaves differently for lm, data.frame, and numeric—method dispatch via UseMethod internally.
Important interview questions and answers
- Q: S3 vs S4?
A: S3 informal attributes; S4 slots and validity—S3 dominates base stats. - Q: class() purpose?
A: Returns class vector driving method dispatch and print formatting.
Self-check
- What does class(fit) show for lm?
- Name one S3 generic function.
Tip: summary(), print(), and plot() are S3 generics—behavior depends on class.
Interview prep
- S3 generic example?
print,summary,plotdispatch on class.