S4 is "formal", so you need to declare inheritance:

    setOldClass(c("SubClass", "SuperClass"))

This usage (first argument of length greater than one) is documented by
help("setOldClass") including among the examples where analogously I see

    setOldClass(c("mlm", "lm"))

and there are more examples in the list of old classes defined by 'methods',
exported as '.OldClassesList'.

Mikael


Date: Thu, 11 Dec 2025 17:10:28 -0500
From: Russell Almond<[email protected]>

I noticed this problem while working with R6 objects, but I can
reproduce it using S3 classes.

If I define a method on the S3 superclass, the subclass doesn't inherit it.

``` r
library(methods)

SuperClass <- function(n) {
    result <- list(n=n)
    class(result) <- "SuperClass"
    result
}

setOldClass("SuperClass")

setGeneric("sampleSize",function(x) {standardGeneric("sampleSize")})
#> [1] "sampleSize"

setMethod("sampleSize","SuperClass", function(x) {x$n})

SubClass <- function (n) {
    result <- SuperClass(n)
    class(result) <- c("SubClass",class(result))
    result
}

setOldClass("SubClass")

example <- SubClass(7)
class(example)
#> [1] "SubClass"   "SuperClass"
is(example,"SuperClass")
#> [1] TRUE
methods("sampleSize")
#> [1] sampleSize,SuperClass-method
#> see '?methods' for accessing help and source code
sampleSize(example)
#> Error: unable to find an inherited method for function 'sampleSize'
for signature 'x = "SubClass"'
```

<sup>Created on 2025-12-11 with [reprex
v2.1.1](https://reprex.tidyverse.org)</sup>

I'm using R 4.5.2 and the same version of methods.

Is this a bug?

      --Russell


-- Russell Almond https://ralmond.net/ [[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to