R – Inheritance
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 classstudent <- 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 students <- list(name = "Utkarsh", age = 21, rno = 96, country = "India") # Derive from class studentclass(s) <- c("InternationalStudent", "student") cat("The method print.student() is inherited:\n")print(s) # Overwriting the print methodprint.InternationalStudent <- function(obj){cat(obj$name, "is from", obj$country, "\n")} cat("After overwriting method print.student():\n")print(s) # Check imheritancecat("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 classsetClass("student", slots = list(name = "character", age = "numeric", rno = "numeric") ) # Defining a function to display object detailssetMethod("show", "student", function(obj){ cat(obj@name, "\n") cat(obj@age, "\n") cat(obj@rno, "\n") } ) # Inherit from studentsetClass("InternationalStudent",slots = list(country = "character"),contains = "student") # Rest of the attributes will be inherited from students <- 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 classstudent <- 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 classInternStudent <- 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 objects <- 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


Please Login to comment...