Skip to content
Related Articles

Related Articles

R – Inheritance

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Jun, 2020
View Discussion
Improve Article
Save Article

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:




# Create a function to create an object of class
student <- function(n, a, r){
  value <- list(name = n, age = a, rno = r)
  attr(value, "class") <- student
  value
}
  
# Method for generic function print()
print.student <- function(obj){
  cat(obj$name, "\n")
  cat(obj$age, "\n")
  cat(obj$rno, "\n")
}
  
# Create an object which inherits class student
s <- list(name = "Utkarsh", age = 21, rno = 96,
          country = "India")
  
# Derive from class student
class(s) <- c("InternationalStudent", "student")
  
cat("The method print.student() is inherited:\n")
print(s)
  
# Overwriting the print method
print.InternationalStudent <- function(obj){
cat(obj$name, "is from", obj$country, "\n")
}
  
cat("After overwriting method print.student():\n")
print(s)
  
# Check imheritance
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:




# Define S4 class
setClass("student",
         slots = list(name = "character"
                      age = "numeric", rno = "numeric"
)
   
# Defining a function to display object details
setMethod("show", "student",
          function(obj){
            cat(obj@name, "\n")
            cat(obj@age, "\n")
            cat(obj@rno, "\n")
          
)
   
# Inherit from student
setClass("InternationalStudent",
slots = list(country = "character"),
contains = "student"
)
   
# Rest of the attributes will be inherited from 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:




# Define class
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
     }
   )
)
  
# Inheriting from Reference class
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
   }
   
)
  
# Create object
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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!