Table of contents
1.
Introduction📑
2.
Inheritance Class of Ruby
3.
Understanding with Examples🕵️‍♀️
4.
Example 1
4.1.
Code💻👇
4.2.
Output
5.
Example 2
5.1.
Code💻👇
5.2.
Output
6.
Explanation🧠
7.
Advantages of Inheritance in Ruby
8.
Frequently Asked Questions
8.1.
What is Ruby?
8.2.
What is meant by inheritance?
8.3.
What are other object-oriented programming languages other than Ruby?
8.4.
What is the syntax for writing an inherited class in Ruby?
8.5.
Which type of inheritance is available in Ruby?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Writing an Inherited Class in Ruby

Author Ayushi Goyal
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction📑

Ruby is a high-level, interpreted, and general-purpose programming language. It is also considered a server-side scripting language used in both front-end as ruby codes can be embedded into HTML(Hypertext Markup Language) as well as for the back-end. It is very easy for a new developer to learn Ruby. Robust, open-source, fast processing, flexibility, and consistency are a few of the features of Ruby. It is mainly used for static websites, web servers, web scraping, data processing, and desktop applications.  

In this article, we will discuss writing an inherited class in Ruby, and an example of the same for clear understanding. 

Inheritance Class of Ruby

Ruby implements inheritance of being an ideal object-oriented programming language. Inheritance allows the user to inherit the features of one class (called parent class or base class) into another class(class child or subclass), providing the concept of “reusability”.

  • Parent class The class from which the properties are inherited is known as the parent class, superclass, or base class.
     
  • Child class - The class which is inheriting the properties from the parent class is known as the child class or subclass.  
Inheritance class in ruby

While creating subclasses in Ruby, it simply reopens the existing class and adds a new method. Also, Ruby only supports single inheritance but not the multiple inheritance. Let’s see what is single and multiple inheritance by an example.  

Single Inheritance - It is the simplest of all other inheritance types. The single-parent class inherits the single-child class.

Know about Single Inheritance in Java in detail.

 

Single Inheritance

Multiple inheritance: In multiple inheritance, one class can be inherited by multiple base classes.

Multiple Inheritance

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 1 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

Overriding 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 FAQslearning Rubyofficial documentation (including installations, manuals, and other references), or directly download different versions of Ruby by clicking here.

Thank you 

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSACompetitive ProgrammingJava 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!

Live masterclass