Home »
Ruby Tutorial
Polymorphism using Inheritance in Ruby
By IncludeHelp Last updated : November 16, 2024
In this tutorial, we will study about polymorphism in Ruby. We all know that Ruby is a pure object-oriented language and polymorphism is one of the very important features of any object-oriented language. You must be having a little difficulty in understanding the topic with its title, but do not worry, you will be having your all doubts clear after going through the article. The rest of the content is all about polymorphism in Ruby along with syntaxes and demonstrating program codes for your better understanding.
Ruby Polymorphism Using Inheritance
We all know this but let me repeat this that Polymorphism is an addition of two words which are "poly" and "Morph". Here the word "poly" means many and "Morph" means forms. So collectively we can say that Polymorphism is a property in which something has many forms. SP, in computer terminology, polymorphism can be called as a method in which a user can invoke the same method passing different objects as parameters.
Polymorphism is a property in which classes have different types of functions but they share the same interface. Polymorphism can be studied under two types of sub-categories which are namely
- Polymorphism with the help of Inheritance
- Polymorphism with the help of Duck-Typing
In this tutorial, we will only study a subcategory which is Polymorphism with the help of Inheritance.
Let us understand Inheritance first, it is a property in which child class inherits the property of parent class.
Implementation of Polymorphism
You can understand the implementation of polymorphism with the help of inheritance with the help of following program code,
Example
=begin
Ruby program to demonstrate polymorphism
with Inheritance
=end
class Fruit
def tastetype
puts "Sour fruit."
end
end
class Orange<Fruit
def tastetype
puts "Sour-Sweet Fruit"
end
end
class Apple<Fruit
def tastetype
puts "Sweet Fruit"
end
end
fruit = Fruit.new
fruit.tastetype
fruit = Orange.new
fruit.tastetype
fruit = Apple.new
fruit.tastetype
Output
Sour fruit.
Sour-Sweet Fruit
Sweet Fruit
Explanation
With the help of the above code, you must have got a clear understanding of Polymorphism with the help of Inheritance. Here the method tastetype is invoked with the help of objects Orange and Apple. The Orange and Apple classes have the same parent class fruit. tastetype is the method which they have inherited from their parent class Fruit.
Implementation of Polymorphism Using Duck Typing
Now, let us understand Duck Typing, it is nothing but an idea to see what can be done by an object instead of what it can do or in the other words you can say that what can be implemented on the object rather than on the class of the instance.
You can understand the implementation of polymorphism with the help of duck typing with the help of following program code,
Example
=begin
Ruby program to demonstrate polymorphism
using duck typing
=end
class College
def enters
puts "A student enters"
end
def type(student)
student.type
end
def course(student)
student.course
end
end
class Ug
def type
puts "student is a under graduate student"
end
def course
puts "BCA student"
end
end
class Pg
def type
puts "student is a post graduate student"
end
def course
puts "MCA student"
end
end
college = College.new
puts "This student is Under graduate"
stu = Ug.new
college.type(stu)
college.course(stu)
puts "This student is Post graduate"
stu = Pg.new
college.type(stu)
college.course(stu)
Output
This student is Under graduate
student is a under graduate student
BCA student
This student is Post graduate
student is a post graduate student
MCA student
Explanation
With the help of the above code, you must have got a clear understanding of Polymorphism with the help of Duck Typing. Here the object stu is playing a role in working with the functionalities of the class College such as its type and its course.