R – Inheritance
Last Updated :
22 Jun, 2020
Inheritance is one of the concept in object oriented programming by which new classes can derived from existing or base classes helping in re-usability of code. Derived classes can be the same as a base class or can have extended features which creates a hierarchical structure of classes in the programming environment. In this article, we’ll discuss how inheritance is followed out with three different types of classes in R programming.
Inheritance in S3 Class
S3 class in R programming language has no formal and fixed definition. In an S3 object, a list with its class attribute is set to a class name. S3 class objects inherit only methods from its base class.
Example:
student <- function(n, a, r){
value <- list(name = n, age = a, rno = r)
attr(value, "class") <- student
value
}
print.student <- function(obj){
cat(obj$name, "\n")
cat(obj$age, "\n")
cat(obj$rno, "\n")
}
s <- list(name = "Utkarsh", age = 21, rno = 96,
country = "India")
class(s) <- c("InternationalStudent", "student")
cat("The method print.student() is inherited:\n")
print(s)
print.InternationalStudent <- function(obj){
cat(obj$name, "is from", obj$country, "\n")
}
cat("After overwriting method print.student():\n")
print(s)
cat("Does object 's' is inherited by class 'student' ?\n")
inherits(s, "student")
|
Output:
The method print.student() is inherited:
Utkarsh
21
96
After overwriting method print.student():
Utkarsh is from India
Does object 's' is inherited by class 'student' ?
[1] TRUE
Inheritance in S4 Class
S4 class in R programming have proper definition and derived classes will be able to inherit both attributes and methods from its base class.
Example:
setClass("student",
slots = list(name = "character",
age = "numeric", rno = "numeric")
)
setMethod("show", "student",
function(obj){
cat(obj@name, "\n")
cat(obj@age, "\n")
cat(obj@rno, "\n")
}
)
setClass("InternationalStudent",
slots = list(country = "character"),
contains = "student"
)
s <- new("InternationalStudent", name = "Utkarsh",
age = 21, rno = 96, country="India")
show(s)
|
Output:
Utkarsh
21
96
Inheritance in Reference Class
Inheritance in reference class is almost similar to the S4 class and uses setRefClass() function to perform inheritance.
Example:
student <- setRefClass("student",
fields = list(name = "character",
age = "numeric", rno = "numeric"),
methods = list(
inc_age <- function(x) {
age <<- age + x
},
dec_age <- function(x) {
age <<- age - x
}
)
)
InternStudent <- setRefClass("InternStudent",
fields = list(country = "character"),
contains = "student",
methods = list(
dec_age <- function(x) {
if((age - x) < 0) stop("Age cannot be negative")
age <<- age - x
}
)
)
s <- InternStudent(name = "Utkarsh",
age = 21, rno = 96, country = "India")
cat("Decrease age by 5\n")
s$dec_age(5)
s$age
cat("Decrease age by 20\n")
s$dec_age(20)
s$age
|
Output:
[1] 16
Error in s$dec_age(20) : Age cannot be negative
[1] 16
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...