Updated April 4, 2023
Introduction to Inheritance in Ruby
In Ruby Inheritance is a very powerful feature, it allows us to use one class attributes like methods and variable inside another class, or in more technical word with the help of Inheritance concept one class A can inherit all the properties of class B, Ruby does not support multiple class inheritance (class A can not inherit properties of many classes like B, C, and D), this concept is useful in case we wanted to reuse the same piece of code for other places without writing the same code, or reusing the same piece of code by doing little modification in the parent class properties instead of writing complete properties.
Below is the syntax for the inheritance in the Ruby, we can explain the below syntax in the following steps.
- First, we have defined a MainClass which holds a method super_class_method.
- Next, we defined the SubClass and this subclass is inheriting the properties of the MainClass (SubClass < MainClass).
- Finally, we have created the object of the SubClass and are able to access the super_class_method with the object SubClass, which shows that we are accessing the parent class properties.
#Defining the MainClass class MainClass # Define the constructor work here it will call automatically on object creation of this class def initialize end def super_class_method #write the code here for the super class as this can be used by child classes. end End #Inheriting MainClass class class SubClass < MainClass def initialize end end MainClass.new subObject = SubClass.new subObject.super_class_method
How does Inheritance Work in Ruby?
We can explain the working of the Inheritance in Ruby with the help of the below flowchart.
- Here class A is the parent class and it contains the method a.
- Class B and Class C are the subclass which are inheriting the properties of class A.
- When we use the command B < A and C < A, class C and class B will get the reference of the attributes stored for method A.
- If we create an object of class B or an object of class A then with that object we can access the methods of class A.
See the below diagram to understand the working of Ruby Inheritance.
Examples to Implement of Inheritance in Ruby
Below are the examples of Inheritance in Ruby:
In the below example we have written code to access methods of one class into another, we can explain the below example in the following steps.
- First, we have defined a class with name MainClass and it contains initialization and a method super_class_method.
- Again we have defined another class with the name SubClass , and this class is inheriting the mainClass(SubClass < MainClass).
- Finally, we created the object of class SubClass and were able to access the method super_class_method.
#Defining the parent class or the class which is going to inherit class MainClass #All the initialisation will be here def initialize puts "Initialisation works for MainClass goes here only." end # method of the superclass def super_class_method puts "This is the method for parent class" end end # Defining child class or subclass which will inherit the properties of the above MainClass class SubClass < MainClass #All the initialisation will be here def initialize puts "Initialisation works for SubClass goes here only." end end # Here we are creating an object of MainClass MainClass.new # Here we are creating an object of SubClass subClass = SubClass.new subClass.super_class_method
Here we are trying to override the method of the parent class into the child class, in the below example we can see that the OverRide class has redefined the same method super_class_method inside it and on calling, it returns different output then what we have defined inside the MainClass method. In Ruby, if we redefine a superclass method(parent class method) inside the child class or subclass then it will be given priority.
We can explain the example in the following steps:
- We have defined a class MainClass and it contains a method super_class_method.
- Next, we again defined a class OverRide which is Inheriting the properties of the MainClass.
- Inside the OverRide class, we are redefining the method super_class_method and changing its properties.
- When we are calling the method with the OverRide object we can see the output, it is what we have defined inside the OverRide class instead of what we have defined inside the MainClass.
#Defining the parent class or the class which is going to inherit class MainClass #All the initialisation will be here def initialize puts "Initialisation works for MainClass goes here only." end # method of the superclass def super_class_method puts "This is the method for parent class" end end # Defining child class or subclass which will inherit the properties of the above MainClass class OverRide < MainClass #All the initialisation will be here def initialize puts "Initialisation works for SubClass goes here only." end def super_class_method puts "OverRide super_class_method is overriding the parent super_class_method method" end end # Here we are creating an object of MainClass MainClass.new overRide = OverRide.new overRide.super_class_method
Advantages of Inheritance in Ruby
There are multiple advantages of using Inheritance in the Ruby, they are given below.
- Because one class can use the properties of the other class, it will reduce the code size and allow us to reuse the same code without impacting the main class properties.
- Memory also gets less burden as they do not have to take reference to multiple properties, they just need to associate new classes(which are Inheriting parent) with the existing properties of the parent class.
- Code clarity, with the help of parent and child class concepts one can easily understand the structure of code and flow if one has little understanding of the Inheritance.
From these tutorials we learned some important uses and basic concepts of Inheritance in Ruby with the help of some useful examples, we also learned the working of Inheritance with the help of syntax, here we have given little focus on the uses of Inheritance in real-world use cases also.
We hope that this EDUCBA information on “Inheritance in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.