# An interface: a virtual class and generics
setClass("Shape", representation("VIRTUAL"))
setGeneric("get_area", function(shape) {
standardGeneric("get_area")
})
# [1] "get_area"
setGeneric("get_line_count", function(shape) {
standardGeneric("get_line_count")
})
# [1] "get_line_count"
# Square inherits it and gives the methods
setClass("Square", contains = "Shape",
slots = c(side_length = "numeric"))
setMethod("get_area", "Square", function(shape) {
shape@side_length * shape@side_length
})
setMethod("get_line_count", "Square",
function(shape) 4)
square <- new("Square", side_length = 5)
area <- get_area(square)
# area = 25
line_count <- get_line_count(square)
# line_count = 4
cat("area =", area, "\n")
cat("line_count =", line_count, "\n")