Purpose
To implement class unions

> setClass("A", representation = representation(name = "character"),
+     prototype = prototype(name = "vinay"))
[1] "A"
> getClass("A")
Class "A"
Slots:
Name: name Class: character > setMethod("initialize", "A", function(.Object, ..., who = "vinay") { + .Object@name <- who + .Object + }) [1] "initialize" > x <- new("A", ) > print(x) An object of class "A" Slot "name": [1] "vinay" > setGeneric("getPerson", function(object) standardGeneric("getPerson")) [1] "getPerson" > showMethods("getPerson") Function: getPerson (package .GlobalEnv) <No methods> > setMethod("getPerson", "A", function(object) { + "vinay" + }) [1] "getPerson" > x <- new("A", ) > print(x) An object of class "A" Slot "name": [1] "vinay" > getPerson(x) [1] "vinay" > setClass("Sub1", representation = representation(interest = "character"), + prototype = prototype(interest = "statistics")) [1] "Sub1" > getClass("Sub1") Class "Sub1"
Slots:
Name: interest Class: character > setMethod("initialize", "Sub1", function(.Object, ..., what = "statistics") { + .Object <- callNextMethod() + .Object@interest <- what + .Object + }) [1] "initialize" > x <- new("Sub1") > print(x) An object of class "Sub1" Slot "interest": [1] "statistics" > setClass("Sub2", representation = representation(interest = "character"), + prototype = prototype(interest = "R")) [1] "Sub2" > getClass("Sub2") Class "Sub2"
Slots:
Name: interest Class: character > setMethod("initialize", "Sub2", function(.Object, ..., what = "R") { + .Object <- callNextMethod() + .Object@interest <- what + .Object + }) [1] "initialize" > x <- new("Sub2") > print(x) An object of class "Sub2" Slot "interest": [1] "R"

Defining a abstract class super class for Sub1 and Sub2 and then calling a function base class function on a derived class object.

> setClassUnion("A", c("Sub1", "Sub2"))
[1] "A"
> x <- new("Sub2")
> x
An object of class "Sub2"
Slot "interest":
[1] "R"
> getPerson(x)
[1] "vinay"