Understanding with Examples🕵️♀️
As inheritance is used to implement a new class using an existing one. So, we need to extend or modify the behavior of existing classes. Here also, we are required to implement an inherited class in Ruby.
Let's do the code for writing an inherited class in Ruby👉
Example 1
In this example, we have created constructors of both base and subclass and called base class methods using subclass object.
Code💻👇
# Writing an inherited class in ruby
# Creating base class or parent class
class CodingNinjas
# Constructor of Base class
def initialize
puts "\nBase Class goes here..."
end
# Creating base class method
def method
puts "Base class method..."
end
end
# Creating subclass
class SubClass < CodingNinjas
# Constructor of Subclass
def initialize
puts "\nSub Class goes here..."
end
end
# Creating base class object
CodingNinjas.new
# Creating sub class object
obj = SubClass.new
# Calling base class method using child class object
obj.method

You can also try this code with Online Ruby Compiler
Run Code
Output

Example 2
In this example, we have created constructors of the base class and a method and then overridden that method in the child class. Let's see how can we override the parent class method.
Code💻👇
# Ruby program writing inherited class in Ruby
# Defining aparent class
class Box
# creating constructor
def initialize(length, breadth)
@w, @h = length, breadth
end
# Parent class method
def getArea
@w * @h
end
end
# Defining a subclass
class Reactangle < Box
# Overriding parent class method
def getArea
@area = @w * @h
puts "Reactangle area is : #@area"
end
end
# Create an object of Square class
puts("Enter Length : ")
len = gets
puts("Enter Breadth : ")
breadth = gets
area = Reactangle.new(Integer(len),Integer(breadth))
area.getArea()

You can also try this code with Online Ruby Compiler
Run Code
Output

Explanation🧠
In the first line, we have defined a class named Boc as the base class or parent class, followed up by creating its constructor and a simple method for calculating the area of the box. Constructor is used to initialize ‘w’ and ‘h’ variables with given values.
Next, we created another class named Rectangle inherited by the parent class Box. In this class, we have overridden the parent class method area for calculating the area of a rectangle.
Later, we asked the user to input the length and breadth of the rectangle and called overridden method to print the rectangle area for given values.
Advantages of Inheritance in Ruby
There are numerous advantages of using inheritance in Ruby, and they are given below👇
💥 Reusing the properties of one class in another class will reduce the code size without impacting the main class properties.
💥 Provides code clarity; with the help of parent and child class concepts, one can easily understand code structure and flow.
💥 Less memory is used as we do not need to refer to multiple properties; they need to associate new classes.
Frequently Asked Questions
What is Ruby?
Ruby is a high-level, interpreted, and general-purpose programming language. It is mainly used for static websites, web servers, web scraping, data processing, and desktop applications.
What is meant by inheritance?
Inheritance is the main feature of the object-oriented programming paradigm, which refers to inheriting the behavior and properties of one class into another class.
What are other object-oriented programming languages other than Ruby?
Other than Ruby, the most popular object-oriented programming languages are Java, C#, C++, Python, Php, JavaScript, Perl, Swift, Dart, etc.
What is the syntax for writing an inherited class in Ruby?
As Ruby only implements single inheritance, the syntax for writing an inherited class in Ruby is as follows: Subclass Name < Baseclass Name.
Which type of inheritance is available in Ruby?
Ruby only implements single inheritance and not multiple inheritance.
Conclusion
In this article, we have extensively discussed writing an inherited class in Ruby. We saw examples supported with code written in Ruby language to illustrate writing an inherited class in Ruby. The explanation of the code was also provided to you to make the topic crystal clear.
Also, read other articles on Ruby -
You can explore some more articles on official ruby FAQs, learning Ruby, official documentation (including installations, manuals, and other references), or directly download different versions of Ruby by clicking here.
Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, Java Programming, and Operating System, etc. Have a look at the interview experiences and interview bundle for placement preparations. And also, enroll in our courses and refer to the mock test and problems available.
Happy Coding, Ninja!