R–Inheritance
Overview
In R, inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (the child or subclass) to inherit properties and behaviors from an existing class (the parent or superclass). This enables code reuse and the creation of specialized classes. Inheritance in R is primarily implemented through the use of the extends function or by simply referencing the parent class when defining the child class. Child classes can access the methods and attributes of their parent class and can override or extend them as needed. This hierarchical structure promotes modularity and simplifies code maintenance and organization in R's OOP paradigm.
Introduction to Inheritance in R
Inheritance is a fundamental concept in object-oriented programming (OOP), and it plays a significant role in R's OOP system. In R, inheritance allows you to create new classes (child or subclass) that inherit properties and behaviors from existing classes (parent or superclass). This promotes code reuse and organization, making it easier to manage complex projects.
To implement inheritance in R, you can use the extends function or simply reference the parent class when defining the child class. The child class inherits the methods and attributes of its parent class, which can be extended or overridden to customize their behavior. This hierarchical structure enhances code modularity and simplifies code maintenance, making it a powerful tool for building robust and flexible R programs.
Example:
Output:
Inheritance in S3 Class
Inheritance in S3 classes in R is a mechanism that allows you to create new classes that inherit characteristics from existing classes. S3, which stands for "Simple, Scalable, and Scriptable," is one of the class systems in R's object-oriented programming paradigm.
In S3 inheritance:
-
Defining a Class:
To create a new S3 class, you typically define it using the class() function and assign it attributes using other functions like attr().
-
Inheritance:
Inheritance is not explicitly defined in S3. Instead, it relies on the naming convention of class attributes. If an object's attribute has a class that matches the desired superclass, it is considered an instance of that class. For example, if an object has a class attribute "numeric," it's considered an instance of both the "numeric" and "vector" classes.
Output:
-
Method Dispatch:
Functions in S3 are generic functions that behave differently depending on the class of their input. When you call a function on an S3 object, R looks for a method associated with the class of that object. If a specific method is not found, R looks for more general methods that match the class hierarchy.
S3 classes provide a simple way to implement inheritance, but they lack some of the formal structure and rigor of more complex class systems like S4. It's important to follow naming conventions and be aware of how method dispatch works when using S3 inheritance in R.
Example:
Output:
Inheritance in S4 class
Inheritance in S4 classes in R is a more formal and structured mechanism compared to S3 classes for creating and managing object-oriented hierarchies. S4 classes offer fine-grained control over class definitions, method dispatch, and object behaviour. Here's an overview of inheritance in S4 classes:
-
Class Definition:
In S4, you define classes using the setClass function. Each class explicitly specifies its slots (data members) and can have defined methods (functions associated with the class).
-
Inheritance:
S4 supports explicit and controlled inheritance. You can create a subclass by using the setClass function with the contains argument to specify the parent or superclass. This means that a subclass inherits the slots and methods of its parent class.
-
Method Dispatch:
Method dispatch in S4 is based on the class hierarchy. When a generic function is called on an S4 object, R looks for a method associated with the class of that object. If a specific method is not found, R searches for methods in the superclass hierarchy until it finds a matching method or reaches the top-level generic function.
-
Slot Access Control:
S4 classes allow you to define access control for slots, specifying whether a slot is readable, writable, or both. This helps enforce encapsulation and data integrity.
-
Formal Documentation:
S4 classes encourage the use of formal documentation through the use of roxygen2-style comments, which makes it easier to understand and maintain complex class hierarchies.
S4 classes are often used for more complex and structured data types and are especially useful when building packages with well-defined and documented object-oriented systems. However, they require a deeper understanding of object-oriented programming concepts and can be more verbose compared to S3 classes.
Example:
Output:
Explaination:
- We define a parent class "Shape" with two slots, "color" and "area."
- We create an object of the "Shape" class called shape.
- We define a child class "Circle" that inherits from "Shape" using the contains argument. The "Circle" class has an additional slot, "radius."
- We create an object of the "Circle" class called circle.
- We define two methods for displaying objects: one for the "Shape" class and another for the "Circle" class, using the setMethod function. These methods customize the output based on the class of the object.
- Finally, we display information about both the "shape" and "circle" objects using the show method. The method dispatch mechanism ensures that the appropriate method is called based on the class of the object, demonstrating inheritance in S4 classes.
Inheritance in Reference Class
Inheritance in reference classes in R is a concept in object-oriented programming (OOP) where you can create new classes that inherit properties and behaviours from existing reference classes. Reference classes, also known as R5 classes, provide a more formal and mutable way to define objects with class-oriented features. Here's an overview of inheritance in reference classes:
1. Defining a Reference Class:
To create a reference class, you use the setRefClass function, which allows you to specify slots (data members) and methods (functions) associated with the class.
2. Inheritance:
Reference classes support explicit and controlled inheritance. You can create a subclass by defining a new reference class that explicitly inherits from a parent reference class using the contains argument in the setRefClass function.
3. Method Dispatch:
Method dispatch in reference classes is similar to S4 classes. When you call a method on a reference object, R looks for a method associated with the class of that object. If a specific method is not found, R searches for methods in the superclass hierarchy until it finds a matching method or reaches the top-level generic function.
4. Instance Modification:
Unlike S3 and S4 classes, reference classes allow for mutable instances. This means you can modify the slots of an object without having to create a new instance.
5. Formal Documentation:
Reference classes encourage the use of formal documentation and provide a structured way to define class attributes and behaviors.

Conclusion
- Inheritance is a fundamental concept in R's object-oriented programming paradigm.
- It allows for the creation of new classes (subclasses) that inherit properties and behaviors from existing classes (superclasses).
- In R, you can implement inheritance in S3, S4, or reference (R5) classes, each with its level of formality and control.
- In S3 classes, inheritance is based on naming conventions and is less formal.
- S4 classes offer more formal and structured inheritance, allowing fine-grained control over class definitions and method dispatch.
- Reference classes (R5) provide mutable instances and are useful for scenarios requiring mutable state and encapsulation control.
- Inheritance enhances code reuse, modularity, and organization, simplifying complex project development in R.